i would like to search for a string lets say for “//bazinga” but only if it is not inside quotation marks (single or double).
when i say quotation marks i mean not escaped ones.
for example.
using this as input should return 13 (index of the match found)
"the fox is" //bazinga
and using this as input should return 18 (since the quotation marks around the string are escaped)
"bla bla \"//bazinga\"
using this as input should return -1 (not found)
"bla bla \"//bazinga"
using this as input should return 16 (the first one is inside quotes so dont count)
"bla //bzinga" //bazinga
using either of these as input should return -1 (not found)
"bbbb //bazinga"
'bbbb //bazinga'
You need a bit of parsing to do that. You can search for either escaped quotation marks, a quoted string, or the text that you want to match, and analyse what you get. Something like:
By searching for escaped quotation marks, then quoted strings, then the text, you make sure that a quoted string is not escaped, and that the text is not inside a quoted string.
If any of the mathces is your text, you found it.
Edit
With this input:
I ran the match, and then showed the result using:
Output:
You can use code like this to get the indexes of the actual hits in the matches collection: