1

I need a regular expression that will match a substring within a quoted string and will not match it outside of the quoted string. For example, if the substring is my substring then in the following example it should match the second entry and unmatch the first and last entry:

This entry of my substring is unmatched. "This entry of \"my substring\" is matched." This entry of my substring is unmatched.

Conversely, in the next example, the expression should match the first and last entry and unmatch the second entry:

"This entry of my substring is matched." This entry of my substring is unmatched. "This entry of my substring is matched."

It should also work for the case where the quoted string containing the target substring is surrounded by other quoted strings:

"A quoted string." "This entry of my substring is matched." "A quoted string."
jvisprime
  • 35
  • 6
  • In your second example there is no double quote ending the last quoted string. Is that intensional? – Jerry Jeremiah Aug 26 '22 at 00:29
  • `(?<!\\)".*?(my substring).*?(?:(?<!\\)"|$)` seems to work The match is the quoted string and `my substring` is in the first captured group. https://regex101.com/r/kFKNTk/1 It doesn't work if there is a quoted string before the quoted string that contains `my substring` – Jerry Jeremiah Aug 26 '22 at 00:32
  • @JerryJeremiah, thank you for your comment, it's not perfect because of the scenario you mentioned, but might be useful in some circumstances. I added the case with the preceding quoted string to my question, and I added a quote where it was missing (thanks to your comment). – jvisprime Aug 26 '22 at 15:36
  • @JerryJeremiah, what is the purpose of the question mark in the `.*?` fragment that appears twice in your pattern? – jvisprime Aug 26 '22 at 15:49
  • https://stackoverflow.com/questions/68568137/regex-match-every-string-inside-double-quotes-and-include-escaped-quotation-mark – Poul Bak Aug 26 '22 at 15:59
  • @jvisprime `.*` will match as much as it can regardless of what those characters are. o if your string was `A quote (") delimits this string: "some stuff my_substring more stuff" and the rest` then the first quote would be considered the start of the quoted string even though it clearly isn't. `.*?` will match only as much as necessary which will choose the closes quotes to my_substring. – Jerry Jeremiah Aug 28 '22 at 22:01

0 Answers0