1

I need to match a string that is in quotations, but make sure the first quotation is not escaped.

For example: First \"string\" is "Hello \"World\"!"
Should match only Hello \"World\"!

I am trying to modify (")(?:(?=(\\?))\2.)*?"

I tried adding [^\\"] to ("), and that kinda works, but it matches either only (") or every other letter that isn't (\") and I can't figure out a way to modify ([\\"]") to only match (") if it is not (\")

This is what I have so far ([^\\"]")(?:(?=(\\?))\2.)*?"

I've been trying to figure it out using these two pages, but still cannot get it.
Can Regex be used for this particular string manipulation?
RegEx: Grabbing values between quotation marks

Thanks

Community
  • 1
  • 1
jao
  • 1,194
  • 1
  • 11
  • 17

1 Answers1

2

You can use negative look behind like this:

(?<!\\)"(.*?)(?<!\\)"

Check see it in action here on regex101

The first match group contains:

Hello \"World\"!
Michael
  • 2,631
  • 2
  • 24
  • 40
  • This expression, however, will not work if you have multiple _inline_ strings. – Michael Sep 12 '12 at 04:30
  • This won't work if there is a string like: `"ended by backslash\\"`. It needs to check if there's an odd number of backslashes preceding. – porges Sep 12 '12 at 04:45
  • @Porges like tripple nested values or what? As far as I know it is not possible with regex to do that kind of (dynamic) matching!? - furthermore I would consider if I was using the right data structure :) – Michael Sep 12 '12 at 04:48