0

Take the following regular expression:

(?<=(&lt;|<)ref)

This will fail the QRegularExpression::isValid(); and QRegularExpression::errorString(); will output

lookbehind assertion is not fixed length

Now apparently not all Regular Expression engines have this limitation, but apparently this one does.

Perhaps there is a Regex oriented workaround for this? And if not, what is the optimal and cleanest strategy in achieving this functionality with the Qt framework?

Anon
  • 2,267
  • 3
  • 34
  • 51

1 Answers1

1

Since you are using the PCRE engine, the length of the pattern inside lookbehind is not fixed. Alternatives may be of different length BUT cannot have nested alternation groups even if their length is also known (fixed).

Thus your (?<=(&lt;|<)ref) can be written as (?<=&lt;ref|<ref). However, a more flexible solution here would be using \K: (?:&lt;|<)ref\K. Here, the nin-capturing group will match &lt; or < and then after matching ref all matched text will be cleared.

Anon
  • 2,267
  • 3
  • 34
  • 51
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563