Search code examples
regexflex-lexer

multiline regex match pattern in brackets ignoring escaped brackets


I finally managed to find out how to do this, see https://regex101.com/r/Zls9Kq/5/

(?!\\)[{](.|\n)*?(?<!\\)[}]

Trying to match

sometext {
 blah \{
\}
\{
 \}
}

But that expression can't be used in flex.

Anyone know if it can be transformed in a way that will work in flex?


Solution

  • I'm going to assume a limited regex engine and give you this to try out

    ([^\\]|^)[{]([^\\}]|\\(.|\n))*[}]

    https://regex101.com/r/WBFbWw/1

    **

     ( [^\\] | ^ )      # (1), Before the opening {, match not an escape or the beginning of the string
     [{]                # Opening brace
     (                  # (2 start)
          [^\\}]             # Not an escape nor a closing brace
       |  \\                 # Or, an escape anything
          ( . | \n )         # (3)
     )*                 # (2 end), 0 to many times
     [}]                # Closing brace