* William ([EMAIL PROTECTED]) [09 Jun 2001 23:26]:
> hi,
> i want to be able to read a text file and extract only the valid
> ip addresses. however, along with valid ip addresses my code is
> grabbing "periods" and invalid ip addresses, e.g., .xxx,
> www.xxx.yyy . how can i correct this?
It might be worth examining the example chapter from "Mastering Regular
Expressions" available from the O'Reilly site.
http://www.oreilly.com/catalog/regex/chapter/ch04.html
Approaching the bottom of the page is a whole example devoted to
matching IP addresses (the section entitled, somewhat appropriately
"Matching an IP address").
I found it useful when doing what you're doing.
I currently use the regexp:
our $ip_RE = qr/
(?:[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])
/xo;
Which I can then use like:
my ($ip) = $line =~ /^Oh my, my ip is ($ip_RE) today\.$/;
(may need more parens)
cheers,
--
iain. <http://eh.org/~koschei/>