0

how to detect a word/phrase written between double quotes using regular expression . if the same word is written outside the double quotes it should not be considered.

Some test cases:

dim a as string = " test"  ( test should be detected)
dim a as string = "abc  test xyz"  ( test should be detected)
dim test as string = "abc"  ( test should not be detected)
dim str as string = "select id from " & test & "where place" (test should not be detected)

Below regex tested, but didn't worked :

"([^"]*test)"
"(.*test?)"
"(.*test.*?[^\\])"
\"([^\"]*?test[^\"]*?)\"

Is there any option to find text inside double quotes?

1 Answers1

0

An approach is to first design a regex that matches when the opposite is true, i.e. when there is no "test" between quotes:

^(?:\\.|[^"]|"(?:\\.|(?!test)[^"])*")*$

Explanation:

  • \\. matches an escape pair. If this is not the case, then:

  • [^"] matches any character that is not a double quote. If this is not the case, then:

  • " " matches a quoted substring, where:

    • \\. matches again an escape pair
    • (?!test)[^"] matches any other character that is not a double quote, but it cannot be a "t" either when that is the start of "test".
  • These matching options are repeated with * and must continue to give a match until the end of the input string ($)

Now we can invert that regular expression, by making it a negative look-ahead that is made at the very start of the input. The .* at the end is optional. Without it, the match is a success, but with a zero length match. If you add it, then the whole input is matched. If your input is multiline, then use the s flag so . will also match newline characters:

^(?!(?:\\.|[^"]|"(?:\\.|(?!test)[^"])*")*$).*

View it on your test cases: regex101

trincot
  • 317,000
  • 35
  • 244
  • 286