Re: Regular Expression and Useage
- From: Paul Lalli <mritty@xxxxxxxxx>
- Date: Tue, 30 Oct 2007 12:41:27 -0700
On Oct 30, 1:54 pm, "inderpau...@xxxxxxxxx" <inderpau...@xxxxxxxxx>
wrote:
I'm somewhat new to regular expression and want to know how to extract
any strings which match an IP address.
I found this on the net and wanted to know if this is the most
efficient (easiest/shortest) way to write the expression or
pattern to match.
Shouldn't you first be concerned about whether it's the most *correct*
before you worry about efficiency?
Also in the discovered solution why do they use the \b word
boundary switch since the characters are of a numeric type ?
I'm not sure about this.
A "word" character in Perl is any letter, number, or underscore.
Therefore, the \b prevents other numbers from being next to the IP
address. That is, it prevents 921128.0.0.123423 from matching. Of
course, it also prevents HOME128.0.0.1, which may or may not be what
you want.
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
It could be shortened to:
\b(?:\d{1,3}\.){3}\d{1,3}\b
Or you could/should use Regexp::Common from CPAN and just write:
/\b$RE{net}{IPv4}\b/
Which not only is more easily readable, but also prevents such false-
matches as 318.99.183.999. That is, it takes care of checking the
individual components' sizes for you.
Paul Lalli
.
- Follow-Ups:
- Re: Regular Expression and Useage
- From: inderpaul_s@xxxxxxxxx
- Re: Regular Expression and Useage
- From: inderpaul_s@xxxxxxxxx
- Re: Regular Expression and Useage
- References:
- Regular Expression and Useage
- From: inderpaul_s@xxxxxxxxx
- Regular Expression and Useage
- Prev by Date: Re: Regular Expression and Useage
- Next by Date: Re: How to convert timestamp to epoch?
- Previous by thread: Re: Regular Expression and Useage
- Next by thread: Re: Regular Expression and Useage
- Index(es):
Relevant Pages
|
|