Trying to use QregularExpression
-
Hi,
I migrated to Qt 6.2.4 and now found that QRegExp has been deprecated and we should now use QRegularExpression.
I have code using QRegExp which worked fine but when I try to replace it with a QRegularExpression it does not return the correct results. In otherwords not all the IPs are returned
I have a QString which contains multiple IPs and I would like to extract all the IPs from the string. Some may repeat and some may not.
This code works fine using QRegExp:
QRegExp rx("(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"); QStringList initialList ; while ((pos = rx.indexIn(msg, pos)) != -1) { initialList << rx.cap(1); pos += rx.matchedLength(); }
When I try to do the following using QRegularExpression it does not return all the IP's.
static QRegularExpression ipRX("(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"); QRegularExpressionMatch match = ipRX.match(msg); QStringList initialList ; if(match.hasMatch()) { initialList << match.capturedTexts(); initialList.removeDuplicates(); }
Any ideas what I'm doing wrong?
Thanks.
-
QRegularExpression::match
only looks for the first match.capturedTexts()
returns the capture groups in that single match.A code equivalent to the QRegExp one would be something like this:
QRegularExpressionMatchIterator i = ipRX.globalMatch(msg); while(i.hasNext()) { initialList << i.next().captured(1); }