QRegExp help
-
I'm trying to separate all integers from a string. The string could look like c1:c2?c3 and I would like to get 1, 2, 3. Currently, I have:
QRegExp xRegExp("[0-9]"); xRegExp.indexIn(string); QStringList list = xRegExp.capturedTexts();
but, it's only capturing the first number. How do I make it capture all occurrences?
-
Hi,
First thing first: are you using Qt 4 ? If not then please move to QRegularExpression as QRegExp has been deprecated and will be removed with Qt6.
QRegularExpression::match is what you are looking for.
-
@SGaist said in QRegExp help:
QRegularExpression::match is what you are looking for.
Cheers, solved by:
QRegularExpression rx("[0-9]"); QRegularExpressionMatchIterator i = rx.globalMatch(string); int z=1; static int nums[6]={0, 0, 0, 0, 0, 0}; while (i.hasNext()) { QRegularExpressionMatch match = i.next(); nums[z++] = match.captured(0).toInt(); } return nums;
-
You know that:
- as soon as your string contains more than 5 numbers you are going to crash ?
- if it contains less that 6 number on a second pass it's going to contain old values ?
- that the first value will always be 0 because z start at one
-
@Inzinejkr said in QRegExp help:
as soon as your string contains more than 5 numbers you are going to crash ?
It never will.
Then you should add an assert that ensures that. Never has a tendency to not hold when programming. A comment would be nice as well because the future you or person who's going to inherit that code might have to do spelunking to fully understand why it was implemented that way.
@Inzinejkr said in QRegExp help:
that the first value will always be 0 because z start at one
That is intended.
Again, put a comment that says so for the same reason as above.