Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(shockme) Re: Regex for IP and Optional Port

by shockme (Chaplain)
on Jan 16, 2002 at 02:10 UTC ( [id://139054]=note: print w/replies, xml ) Need Help??


in reply to Regex for IP and Optional Port

Thanks for the insight, everyone. After studying andye's and dmmiller2k's suggestions, I was able to come up with a somewhat functioning framework. It was tye's comment that really brought it all home, though. \w does not include spaces, but it does include numbers. Doh! (Upon reflection, my example input data file is a very poor example, indeed.) The solution lied in constructing a character class of [\s\Wa-zA-Z]+.

For completeness' sake, following is the final regex:

( # $1 - first IP \d{1,3} # 1-3 digits (?: # grouping for rep count \. # dot \d{1,3} # 1-3 digits ){3} # three more times (?: # optional extra grouping \/ # a slash \d+ # 1st port number )? # close extra grouping ) # close group around 1st IP [ # set up character class \s # whitespace a-zA-Z # letters \W # non-word characters ]+ # end character class ( # $2 - 2nd IP \d{1,3} # 1-3 digits (?: # grouping for rep count \. # dot \d{1,3} # 1-3 digits ){3} # three more times (?: # optional extra grouping \/ # a slash \d+ # 2nd port number )? # close extra grouping ) # close group around 2nd IP /x ) # specify /x

Thanks again!

Update: Fiddled with the grammer.

If things get any worse, I'll have to ask you to stop helping me.

Replies are listed 'Best First'.
Re: Re: Regex for IP and Optional Port
by spaz (Pilgrim) on Jan 17, 2002 at 04:03 UTC
    Keep in mind that a valid IP address is matched by this beast, as found in the Ram Book:
    m/^([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])\. ([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])$/;
    Since this is a rather unwieldy expression, and rather static, it should be quite easy to compile it once with qr// and insert it as needed.

    -- Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://139054]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-25 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found