Home > Enterprise >  Ruby Regular Expression not to match if bracket is escaped
Ruby Regular Expression not to match if bracket is escaped

Time:09-02

I want to extract string surrounded by curly bracket by using regular expression in ruby.
I create the following regular expression:

/\A({[^(})]*})/i

I add ^(}) to regular expression because I want the shortest match.
It means:

target string: {aaa}XXX{aaa}

expected: {aaa}

when I remove `^(})` (the regular expression is `/\A({*})/i`)
I will obtain: `{aaa}XXX{aaa}`

It works very well in many cases.

What I want to do is avoid matching when I escape by using backslash.

target string: "{aaa\}}"

expected match: {aaa\}}

Actual matching result is {aaa\}.
How should I change my regular expression to obtain the expected match?

CodePudding user response:

You can use negative lookbehind to find a closing parenthesis that is not escaped.

{.*?(?<!\\)}

Here it is commented

{ # opening parenthesis
.* # followed by anything
? # match as little as possible. This should cover your shortest match clause.
(?<!\\)} # followed by a closing parenthesis that is not preceded by a backslash.

Do note though, that this will cover escaping, but will not cover double escaping. "\\}" and "\\\\}"are still considered escaped and will not be matched.

CodePudding user response:

You can use

/\A\{[^\\{}]*(?:\\.[^\\{}]*)*}/m

See the Rubular demo.

Details:

  • \A - start of string
  • \{ - a { char
  • [^\\{}]* - zero or more chars other than \, { and }
  • (?:\\.[^\\{}]*)* - zero or more sequences of
    • \\. - any escaped char
    • [^\\{}]* - zero or more chars other than \, { and }
  • } - a } char.

Note that m flag is used to make . match any chars including line break chars.

  • Related