Hi,
the RegExp machine always tries to make a match.
In "blah [10] 24" the "[0-9]+" matches the "1" and the "(?!\\])" matches the "0".
This is correct because "0" is not "]".
To prevent the RE here to match the "0" you either need a possessive quantifier (like (?>) in Perl or ++ in Java, but sadly not available in QRegExp) or you also need to exclude the digits in your lookahead. Use "(?![]0-9])" here.
Because there also exists no lookbehind you need a "[^[0-9]" to exclude a leading "[". To also match at the beginning of the string you need to add a "^|" in the beginning.
Note that now the character before the 'wanted match' is part of the match itself, e.g. you get " 99", not only "99".
Where required capture the digits using () and use .cap(1).
HTH, Bernd
--
int main(void)
{
QString str
= "Some text [12] and [14] numbers 99 [231] 7";
QRegExp rx
("(?:^|[^[0-9])([0-9]+)(?![]0-9])");
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1)
{
qDebug() << rx.cap(1);
pos += rx.matchedLength();
}
}
int main(void)
{
QString str = "Some text [12] and [14] numbers 99 [231] 7";
QRegExp rx("(?:^|[^[0-9])([0-9]+)(?![]0-9])");
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1)
{
qDebug() << rx.cap(1);
pos += rx.matchedLength();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks