CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Regex for ip address

 



jain236
New User

Mar 31, 2010, 7:39 AM

Post #1 of 4 (4167 views)
Regex for ip address Can't Post

Hi i am try to use regex to find id addres in the below way.

<c>

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

</c>

in the above one \d{1,3} is repeating so i thought of writing it using \1 in below manner

<c>

/(\d{1,3)\.\1\.\1/

</c>

it is not working , can any one help me ? or any better solution


roolic
User

Apr 1, 2010, 11:20 PM

Post #2 of 4 (4131 views)
Re: [jain236] Regex for ip address [In reply to] Can't Post

shorter version of general check:
/(?:\d{1,3}\.){3}\d{1,3}/

\1 means '1' string


tej.87g
Novice

Aug 29, 2010, 4:02 AM

Post #3 of 4 (3333 views)
Re: [jain236] Regex for ip address [In reply to] Can't Post


In Reply To
Dont use regex for parsing ip address:
check this link
http://www.perlmonks.org/?node_id=221512
that may help you



rayapallisuresh
New User

Dec 14, 2010, 11:03 AM

Post #4 of 4 (2976 views)
Re: [jain236] Regex for ip address [In reply to] Can't Post

ip address can be in any form, what we are discussing here called human readable form. ex: 10.12.45.56
the regex like /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ can match those type of addresses but the problem is it matches for 999.999.999.999, which is not a valid ip address.

below regex matches only for valid ipaddress in dot notation:

/^(2[0-5][0-5] | 1[0-9][0-9] | [1-9][0-9] | [1-9]) \.
(2[0-5][0-5] | 1[0-9][0-9] | [1-9][0-9] | [0-9]) \.
(2[0-5][0-5] | 1[0-9][0-9] | [1-9][0-9] | [0-9]) \.
(2[0-5][0-5] | 1[0-9][0-9] | [1-9][0-9] | [0-9])$/x

please correct me if wrong

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives