PDA

View Full Version : Does QRegExp::lastIndexIn work correct?



Nebelig
20th June 2011, 11:48
#include <QCoreApplication>

#include <QRegExp>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);


QRegExp reg("(.*)");

QString str = "_1234_4567_";

int pos1 = reg.indexIn(str, 5);
qDebug() << "F" << pos1 << reg.cap();

int pos2 = reg.lastIndexIn(str, 5);
qDebug() << "B" << pos2 << reg.cap();

return a.exec();
}


Generates the following output:



F 5 "_4567_"
B 5 "_4567_"


But I think output of reg.lastIndexIn(str, 5) must be like this:



B 0 "_1234_"


Hm....

Rachol
20th June 2011, 11:51
It is totally correct, since the search in the string starts from after 5th character.

Nebelig
20th June 2011, 11:58
It is totally correct, since the search in the string starts from after 5th character.
Thanks.
But, lastIndexIn() not find a match backwards from offset?

Rachol
20th June 2011, 11:59
Unfortunately, not.

Edit. Sorry I was thinking about indexIn...

So, yes it is searching backwards, but it doesn't disqualify the characters after the offset. So in your case the regular expresion is matching the string starting from offset = 5, and that's what is returned.

MarekR22
20th June 2011, 12:28
reg.lastIndexIn(str, -6);

Rachol
20th June 2011, 12:34
reg.lastIndexIn(str, -6);

It is equal to
reg.lastIndexIn(str, 5);

Nebelig
20th June 2011, 13:34
OK. Any suggestion about backward capturing using QRegExp? In my example I want get "_1234_" from position 5.

Rachol
20th June 2011, 13:40
What exactly do you want to capture?

MarekR22
20th June 2011, 13:48
Exactly explain what you are try to do (not how you are trying to achieve that).
I have gut feeling that you just need something like that (especially that your regular excretion is just a wild card):

QStringList items = str.split("_", QString::SkipEmptyParts);

Nebelig
20th June 2011, 15:12
An example, a cursor in texteditor is inside the keyword:


blablabla get-deffunct|ion-list blalblabla (| <- cursor)

and I want detect is the word correct (get-deffunction-list) or not, if it is correct I need capture it.

It was a good idea for use reg expression for capturing left part and if it was correct get right part of word.

Searching word boundary (space, tabs etc) not a good idea, becouse:


bla blablabla>=get-deffunction-list!=blalblabla bla

is correct to.

MarekR22
20th June 2011, 17:41
1. if you have editor then you should use QTextDocument::find-4 (http://doc.qt.nokia.com/latest/qtextdocument.html#find-4)
2. in regular expression there is assertion "\b" which means word boundary (you don't have to use space or other character to detect that), see QRegExp (http://doc.qt.nokia.com/latest/qregexp.html) documentation.
example: QRegExp("\\b\\w+\\b");
3. check qt examples there is something similar to your problem.

Nebelig
21st June 2011, 10:42
MarekR22, thanks for the advice.