1

I can find previous match with this, but what I can't do, is to capture the length of the matched string:

int pos = 0;

if((pos = text.lastIndexOf(QRegularExpression(pattern), cursorPosition - 1)) != -1))
    cout << "Match at position: " << pos << endl;

I can capture the length of the match with QRegularExpressionMatch, but I could not find any flag/option in the QRegularExpression nor QRegularExpressionMatch classes that would change the direction of the search. (I don't mean to reverse the pattern, but finding the first match before a certain position in a string.)

Example (I want to find not-even-regex "hello"):

    hello world hello
            ^
          start (somewhere in the middle)

And this should be the matched section:

   hello world hello
   ^   ^
start  end

Thank you in advance.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • What is your question? Your question doesn't make sense. `QString::lastIndexOf(QRegExp, int from)` does exactly what you're asking, which you already state is your answer. You also state that you want to get the length of the string, and then immediately state that that's all you can do. – Phlucious Apr 27 '15 at 17:36
  • Also, why are you running a search of a string using itself as the pattern? Very strange indeed. – Phlucious Apr 27 '15 at 17:37
  • @Phlucious 2. - Sorry, error while renaming. 1. - I want to get the length of the match, **the length of the match**, not just an offset. – LogicStuff Apr 27 '15 at 17:38
  • 1
    Ah, after reading the description of QRegularExpression I realized what you were asking. Looks like a design oversight on Digia's part. – Phlucious Apr 27 '15 at 18:10

2 Answers2

2

Note that in Qt5 QRegExp != QRegularExpression and I'm much more familiar with QRegExp. That said, I can't see a way to do what you want with QRegularExpression or QRegularExpression::match().

I would instead use QString::indexOf to search forwards and QString::lastIndexOf to search backwards. You can do this with either QRegExp or QRegularExpression if you just want to find the offset.

For example,

int pos = 8;
QString text = "hello world hello";
QRegularExpression exp("hello");

int bwd = text.lastIndexOf(exp, pos);   //bwd = 0
int fwd = text.indexOf(exp, pos);       //fwd = 12

//"hello world hello"
// ^       ^   ^
//bwd     pos fwd

You, however, also want to use the captured text, not just know where it is. This is where QRegularExpression seems to fail. As far as I can tell, there is no lastMatch() to retrieve the matched string after calling QString::lastIndexOf() QRegularExpress.

If you use a QRegExp instead, however, you can do this:

int pos = 8;
QString text = "hello world hello";
QRegExp exp("hello");

int bwd = text.lastIndexOf(exp, pos);   //bwd = 0
int fwd = text.indexOf(exp, pos);       //fwd = 12

//"hello world hello"
// ^       ^   ^
//bwd     pos fwd

int length = exp.cap(0).size();     //6... exp.cap(0) == "hello"
//or, alternatively
length = exp.matchedLength();       //6

The QRegExp object that you pass to the QString method gets updated with the captured string(s), which you can then use and manipulate. I can't imagine that they forgot to do that with QRegularExpression, but it looks like they might have.

Phlucious
  • 3,704
  • 28
  • 61
  • Thank you for the info, I didn't know much of `QRegExp` but that it's inferior to `QRegularExpression`. I've realized I need to support lookbehind - isn't there really some way to do that with `QRegularExpression`? – LogicStuff Apr 27 '15 at 20:23
  • From the docs: http://pcre.org/pcre.txt. Do a search for lookbehind assertions. Or here: http://perldoc.perl.org/perlretut.html#Looking-ahead-and-looking-behind – Phlucious Apr 27 '15 at 21:02
  • Since Qt 5.5 there is oveload `int QString::lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const` which allows capturing the matched string. – HiFile.app - best file manager Dec 01 '20 at 20:46
1

It can be done with QRegularExpression. Just use method

QRegularExpressionMatch QRegularExpression::match(const QString &subject, int offset = 0, MatchType matchType = NormalMatch, MatchOptions matchOptions = NoMatchOption) const

and later call methods capturedLen(int), capturedStart(int) and similar for the result.

Links:

http://doc.qt.io/qt-5/qregularexpression.html#match

http://doc.qt.io/qt-5/qregularexpressionmatch.html

9cvele3
  • 313
  • 2
  • 10