Home > OS >  What is an alternative to the negative lookbehind (that's not implemented) in CTRE?
What is an alternative to the negative lookbehind (that's not implemented) in CTRE?

Time:05-03

I'm trying to match a part of a string that surrounds a word inside of it. For example: [This]. I would like to match [This], but only when the character before it is not a backslash, so for example, not this \[This].

for (auto match: ctre::range<R"((((?<!\\))\[[^\]]*\])">(input)) {
   // you can use match.str() here
}

Usually the regex I would use is this:

(((?<!\\))\[[^\]]*\])

..but, according to this comment and the received ctre::problem_at_position<5>, it seems that it's not supported in CTRE as of today.

Is there a way to do what I want to do even though it's not implemented?

CodePudding user response:

The regex is the following:

(?:^|[^\\])(\[[^\]]*\])

where:

  • (?:^|[^\\]) is a non-capturing group that uses alternation (|) for choosing between ^ (the beginning of the string) or [^\\] (any character that is not a backslash).
  • (\[[^\]]*\]) captures the desired pattern.
  • Related