How to get the regular expression matched part
-
Hi
I want to get the address like IP from a string.
e.g. Device address: 192.168.1.1
Local address: 192.168.1.2I have gotten a regular expression
QRegExp("(\\d{1,3}\\.){3}\\d{1,3}")
to match address.
So how to use this regular expression to get the address, not use it to spilt.Regards
Mihan -
-
@Christian-Ehrlicher Thanks,
QRegularExpression
is something I haven't known.What's the different of
QRegExp
andQRegularExpression
-
@Mihan You can also get the address by
QRegExp
.QString device="Device address: 192.168.1.1"; QRegExp rx("(\\d{1,3}\\.){3}\\d{1,3}"); if(rx.indexIn(device) >= 0) { qDebug() << rx.cap(); }
As the doc says
Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp.
You can check this page for the difference of the two classes:
https://doc.qt.io/qt-5/qregularexpression.html#notes-for-qregexp-users -
@Mihan said in How to get the regular expression matched part:
Now I have replaced all the QRegExp with QRegularExpression.
By the way QRegExp has been deprecated by Qt => QTBUG-72974