2

I am trying to match everything except

"""

in Regex. My attempt includes:

[^\"]{3}

This only includes everything except a double quote. I want to also include one double quotes and two double quotes, so that, for instance, this entire string would match:

This example "" would match " all the way.

I am trying to make this work using JFlex.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
adhdj
  • 352
  • 4
  • 17
  • Do you mean `^(?!.*""").*`? Any string not containing a "word"? What is the programming language / regex flavor? What is the final goal? – Wiktor Stribiżew Oct 03 '18 at 18:16
  • This works on regextester.com but results in an "Unterminated string at end of line." error in JFlex. – adhdj Oct 03 '18 at 18:30
  • Sorry. I just realized you commented my answer saying you're using JFlex. I didn't know you were the one who asked the question, just realized it. Since no one answered, I'll try to help you from your tool. – Judismar Arpini Junior Oct 03 '18 at 20:33
  • It doesn't work on flex either, actually. I was double mistaken. Now I really would like to know the solution for this. – Judismar Arpini Junior Oct 03 '18 at 21:21
  • 1
    Thanks for the attempt @JudismarArpiniJunior! I was able to get a less-than-elegant solution involving three regex cases. Answer posted. – adhdj Oct 03 '18 at 22:57
  • Great! I didn't get how to use those cases to build the final solution. Would you mind sharing your final regexp? – Judismar Arpini Junior Oct 04 '18 at 00:43

1 Answers1

2

A solution is to use several regex expressions:

[\"][\"][\"]   -> case (1) for three consecutive double quotes.
[^\"]+         -> case (2) for anything except a double quote
[\"]           -> case (3) grab one double quote

A string with two double quotes will then be "gobbled" one at a time. A string with three double quotes will choose case 1 because of the maximum munch rule and the precedence of case 1 over case 3.

adhdj
  • 352
  • 4
  • 17