PDA

View Full Version : QRegExp doesn't seem to work as it should



thomaspu
21st December 2007, 00:01
I gotta be doing something stupid here. Can anyone lend another pair of eyes?

QString crap = "[01;32msshtest@router[01;34m / $[00m";
Given the above text, I wish to parse out the numbers right after the initial '['
In other words, I want to match this list:
01;
32m
01;
34m
00m

Here's the code:
QRegExp regEx( "(([0-9]+)[;|m])+");
int pos = 0;
QStringList matches;
while ( (pos = regEx.indexIn( crap, pos)) != -1)
{
matches << regEx.cap(1);
printf( "Matched: %s\n", regEx.cap(1).toLatin1().constData() );
pos += regEx.matchedLength() -1;
}

This is what I get when its run:
Matched: 32m
Matched: 34m
Matched: 00m

Umm.... why in the world is it not matching the stuff with ';'s ?

Paul

wysota
21st December 2007, 00:35
Your regexp says : Match at least sequence of at least one digit followed by "m", "|" or ";". What you probably want is "match a number of digits followed by either "m" or ";". To me the expression should be "\d+(m|;)" and you should iterate over the string until you can't match the expression.

thomaspu
21st December 2007, 01:17
Yeah, looks like me regex was off. Using ([0-9]+)[;m]{1,1} works.

Thanks,
Paul

wysota
21st December 2007, 08:49
"{1,1}" - this part is not needed.