regexjflex

Regex match everything except three consecutive double quotes


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.


Solution

  • 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.