Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What regex can I use to match any valid IP-address represented in dot-decimal notation?

What regex can I use to match any valid IP-address represented in dot-decimal notation?

like image 495
planetp Avatar asked Feb 27 '10 09:02

planetp


People also ask

How do I grep a specific IP address?

so you can use grep -oE "\b([0-9]{1,3}\.){ 3}[0-9]{1,3}\b" to grep the ip addresses from your output. Thanks. This works.

How do you represent an IP address in regex?

\d{1,3}\b will match any IP address just fine. But will also match 999.999. 999.999 as if it were a valid IP address. If your regex flavor supports Unicode, it may even match ١٢٣.

Which regex pattern can be used to find IP address?

Approach: Regex (Regular Expression) In C++ will be used to check the IP address. Specifying a range of characters or literals is one of the simplest criteria used in a regex.


1 Answers

if($ip=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255  && $2<=255 && $3<=255  &&$4<=255 )))
     {
         print "valid\n";
     }
     else
     {
         print "Invalid\n";
     }
like image 114
muruga Avatar answered Nov 17 '22 00:11

muruga