Re: Regular Expression and Useage



On Oct 30, 11:41 am, Paul Lalli <mri...@xxxxxxxxx> wrote:
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

Paul does it matter if I'm using these Perl Regular Expressions in C+
+ ? Is the implementation of this RegEx engine not the same uner any
platform ? Its not working in C++.

.



Relevant Pages

  • Re: Regular Expression and Useage
    ... pattern to match. ... boundary switch since the characters are of a numeric type? ... Paul Lalli ...
    (comp.lang.perl.misc)
  • Re: sprintf question
    ... 'tr' is used to translate a group of characters to a corresponding ... group of another characters (think of a simple substitution cypher). ... Paul Lalli, however, claims that it is also more efficient for a simple ...
    (perl.beginners)
  • Re: Using switch
    ... Paul Lalli wrote: ... used vi's search/replace to insert valid perl code. ... > source files." ... Nowhere near 1 million characters! ...
    (comp.lang.perl.misc)
  • Re: file upload
    ... > designing a file upload request, File::Basename don't help you with that. ... happen to contain one of those characters. ... Paul Lalli ...
    (comp.lang.perl.modules)