S
S
SoEltic2014-08-27 14:54:15
HTML
SoEltic, 2014-08-27 14:54:15

How to find ip addresses in free text using QRegExp?

There is a certain html page with the list of ip addresses.
I'm trying to rip out these same addresses from it using Qt, or rather QRegExp.
Googled on the topic, but I just can not get the right answer from QRegExp.
An expression like this:

(?:(?: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]?)
gives an empty response.
This ([0-9]{1,3}[\\.]){3}[0-9]{1,3}gives out only parts of the address, or rather
201.
265.
31.
17.
and so on.
How to write an expression to correctly rip out ip addresses?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mark Filatov, 2016-02-03
@kilatoo

QRegExp rx("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})");

QString text("192.168.1.1 blah-blah 192.168.1.2 blah-blah");
int pos = 0;
QStringList list;
 
while ((pos = rx.indexIn(text, pos)) != -1) 
{
  list << rx.cap(1);
  pos += rx.matchedLength();
}

Result:
192.168.1.1
192.168.1.2

Variant with range check 0-255:
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]?))");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question