QRegularExpression does not capture group correctly
-
Hi!
I have a problem when trying to capture a part of a QString using QRegularExpression.
I want to capture the part 12,0ak from the string, however only 12,0a was captured.QString str{"\t2,23 - 3,20\t12,0aK\t8713-9-7\t2140"}; QRegularExpression rx{R"([\t:](\d{1,2},\d[aKMgL]*)[^\d%]\s)"}; auto match = rx.match(str); qDebug() << match;
QRegularExpressionMatch(Valid, has match: 0:(75, 83, "\t12,0aK\t"), 1:(76, 81, "12,0a"))
When I try the regular expression at https://www.regextester.com/ it finds a match. So is this an error in QRegularExpression, or what I am doing wrong?
Cheers! -
Hi,
Neither https://regextester.com nor https://regex101.com matches anything with the pattern you gave and the test string.
There might be an issue with the code you posted.
If only the
aK
part interest you, you should rather use something like"\\\\t(\\d{1,2},\\d[aKMgL]+)\\\\t"
that you can directly pass to the QRE constructor or the regex101 version:\\t(\d{1,2},\d[aKMgL]+)\\t
.Note that if you want to have letters mandatory after the numbers you have to replace the * by a + otherwise these will be optional and the match might take something else.
-
Hi!
Thank you for taking your time giving an answer.
Well, I tried the same pattern with QRegularExpression, QRegExp and std::regex and I get the same result for all of them, so I guess it is the pattern that is wrong.However, I still don't understand what I am doing wrong with the pattern, but I guess I will have to go back to the school bench...
My task is to capture something like 12,0 or 12,0a or 12,0aK from a larger line read from file. Printing the line (a QString) gives:
5,81%\t910 550\t10 466\t11,88\t15%\t2,23 - 3,20\t12,0aK\t8713-9-7\t2140 : 1
This pattern works:
R"([\t:](\d{1,2},\d[aKMgL]*)[^%\d])"
(will capture 12,0aK)
This pattern does not work:R"([\t:](\d{1,2},\d[aKMgL]*)[^%\d]\s)"
(will only capture 12,0a)
Note the trailing \s in the second example capturing a white space.I tried the second pattern at https://regex101.com/ and it captures 12,0a too. You need to type the pattern as:
[\\t:](\d{1,2},\d[aKMgL]*)[^%\d]\\t
in that case.So, nothing wrong with QRegularExpression, rather the pattern then.