Does QRegExp::lastIndexIn work correct?
Code:
#include <QCoreApplication>
#include <QRegExp>
#include <QDebug>
int main(int argc, char *argv[])
{
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:
Code:
F 5 "_4567_"
B 5 "_4567_"
But I think output of reg.lastIndexIn(str, 5) must be like this:
Hm....
Re: Does QRegExp::lastIndexIn work correct?
It is totally correct, since the search in the string starts from after 5th character.
Re: Does QRegExp::lastIndexIn work correct?
Quote:
Originally Posted by
Rachol
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?
Re: Does QRegExp::lastIndexIn work correct?
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.
Re: Does QRegExp::lastIndexIn work correct?
Code:
reg.lastIndexIn(str, -6);
Re: Does QRegExp::lastIndexIn work correct?
Quote:
Originally Posted by
MarekR22
Code:
reg.lastIndexIn(str, -6);
It is equal to
Code:
reg.lastIndexIn(str, 5);
Re: Does QRegExp::lastIndexIn work correct?
OK. Any suggestion about backward capturing using QRegExp? In my example I want get "_1234_" from position 5.
Re: Does QRegExp::lastIndexIn work correct?
What exactly do you want to capture?
Re: Does QRegExp::lastIndexIn work correct?
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):
Re: Does QRegExp::lastIndexIn work correct?
An example, a cursor in texteditor is inside the keyword:
Code:
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:
Code:
bla blablabla>=get-deffunction-list!=blalblabla bla
is correct to.
Re: Does QRegExp::lastIndexIn work correct?
1. if you have editor then you should use QTextDocument::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 documentation.
example: QRegExp("\\b\\w+\\b");
3. check qt examples there is something similar to your problem.
Re: Does QRegExp::lastIndexIn work correct?
MarekR22, thanks for the advice.