1

my regular expression :(?<=defining\s)[^;]*

should try to find var in the following cases:

   defining var;
some text defining var;

I tested the regular expression using some online tools. Unfortunately it does not work with Qt for some reason. Is there something wrong with the regular expression or should I look for the error somewhere else in the code? It is strange because regular expressions without lookbehind work.

Extra: a bit off topic but because I'm already writing the question: How do I have to change my regular expression so that it can find var in the following case with more then one whitespace:

some text defining      var;

Right now it finds all but one whitespace and var.

user7431005
  • 3,899
  • 4
  • 22
  • 49
  • 1
    According to [this qt documentation page](http://doc.qt.io/archives/qt-4.8/qregexp.html), `Perl's lookbehind assertions, "independent" subexpressions and conditional expressions are not supported.` – ctwheels May 01 '18 at 15:20
  • oh, that is bad. Is there a way to rewrite my regular expression without lookbehind assertions? – user7431005 May 01 '18 at 15:22
  • You could just capture both parts: `(defining\s+)([^;]*)`. Changing `\s` to `\s+` also allows it to match multiple whitespace characters. That would not have been possible with lookbehind assertions, however, since they would have to be fixed width. You would have gotten the whitespace in the match instead of the lookbehind. – ctwheels May 01 '18 at 15:23
  • ok, I think that is what I end up doing. – user7431005 May 01 '18 at 15:25
  • Which version of Qt are you using because QRegularExpression *should* have support for lookbehinds according to [this blog article by the class' author](https://dangelog.wordpress.com/2012/04/07/qregularexpression/). It doesn't change the fact that you're still not going to be able to use variable width lookbehinds, but for curiosity's sake. – ctwheels May 01 '18 at 15:30
  • Do as ctwheels suggested but since you do not need the defined part use a non-captureing group for the prefix. `(?:defining\s+)([^;]*)` – wp78de May 01 '18 at 15:38
  • I use qt 5.9 for my project. Thanks for the tip for the non-capturing group – user7431005 May 01 '18 at 15:46
  • @user7431005 You do not need a non-capturing group. Use `defining\s+([^;]+)` – Wiktor Stribiżew May 01 '18 at 16:37

1 Answers1

1

You can match the context to the left of the value you want to obtain and capture the latter into a capturing group:

QRegularExpression regex("defining\\s+([^;]+)");
QRegularExpressionMatch match = regex.match(str);
QString textYouWant = match.captured(1);

Here, defining\\s+([^;]+) matches defining, then 1+ whitespace chars, and then captures 1+ chars other than ; into Group 1 (that you can access using .captured(1)).

See this regex demo.

Note that QRegularExpression is PCRE-powered, so you may use the PCRE \K operator to get the value you need in the match itself:

QRegularExpression regex("defining\\s+\\K[^;]+");
                                      ^^^

The \K operator discards the text matched so far, so you will only get the text matched with [^;]+ after the match is returned.

See another regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563