Good day. In php I dig a couple of days. I don't quite understand regular expressions and how to work with them, so I ask for help here. Hope for understanding.

There is a specific page (url) with the following content:

TOTAL : 3, id=001, ... IP Address(es): ipv4: 1.2.3.4, ... ipv4: 5.6.7.8, ... ipv4: 12.253.789.25, ... 

With php I need to get every ip-address (1.2.3.4, 5.6.7.8, 12.253.789.25) in the column. So that I can output them in a similar way in my php file:

 1.2.3.4 5.6.7.8 12.253.789.25 

I also want to say that these ip-addresses can be quite a lot, about 1000. I would be happy with any help, because I have been tormented for several days.

  • 12.253.789.25 - IPv4 ??? - user207618
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

 \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} 

In contrast to the solution of UW Qwertiy , this regular expression contains only those values ​​where there are one, two or three digits before, between and after the points, which is expressed by the {1,3} pattern. That is, 12.253.789.25 will be 12.253.789.25 , but not 12.2534.789.25 , - this is exactly what is required for searching for IPv4 addresses. Thank.

  • Thank you, everything is working properly. In the example, I really made a mistake with one ipv4 12.253.789.25, but in reality there will be no such erroneous ip there. - Koko Hekmatyar

Regular Expression for IPv4 Search:

 ((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?) 

You can check here .

    If you do not want to check for validity, then just

     \d+\.\d+\.\d+\.\d+ 
    • This is not IPv4 , but four numbers separated by dots and not saved as links in the search result. - user207618
    • one
      @Other, I know. But usually you should not subtilize, if for this there is no reason. - Qwertiy
    • Well, I don’t know, IPv4 said, so you need to look for what fits this definition and certainly won’t find 0.123.456.789 :) - user207618
    • @Other, but in order for it to find it, it is necessary that it be there :) It is unlikely that there are any random numbers in groups of 4 through a dot. And if they are, then they, too, can form the correct ip-address, then another sign is needed. Conclusion: regular-time adjustment for correct values ​​is needed only if the task is to filter the correct ip-addresses from incorrect ones. In other cases, it is either not required, or it still cannot completely solve the problem. - Qwertiy