Home > Archive > PERL Miscellaneous > October 2007 > Regular Expression and Useage
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Regular Expression and Useage
|
|
| inderpaul_s@yahoo.com 2007-10-30, 7:09 pm |
| 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. 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.
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Many thanks in advance.
Victor
| |
|
| On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
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. 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.
>
> \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
>
> Many thanks in advance.
>
> Victor
Please take a look at the answers to the exact same question you
posted less than 24 hours ago in the thread titled, "Regex Help."
--TV
| |
| Paul Lalli 2007-10-30, 7:09 pm |
| On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
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
| |
| inderpaul_s@yahoo.com 2007-10-30, 7:09 pm |
| On Oct 30, 11:31 am, TonyV <kingskip...@gmail.com> wrote:
> On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
> wrote:
>
>
>
>
>
>
> Please take a look at the answers to the exact same question you
> posted less than 24 hours ago in the thread titled, "Regex Help."
>
> --TV
Sorry to ask but if you can send me the URL to the post I would be
happy to read that. For some reason I have been unable to find my own
post either through my profile or just plain searching the NG where I
posted. Hence the reason for my posting again. I'm not sure what the
issue is.
| |
| inderpaul_s@yahoo.com 2007-10-30, 7:09 pm |
| On Oct 30, 11:41 am, Paul Lalli <mri...@gmail.com> wrote:
> On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
> wrote:
>
>
>
> Shouldn't you first be concerned about whether it's the most *correct*
> before you worry about efficiency?
>
>
> 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.
>
>
> 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
Thanks Paul for the help. I tested the above solution the one I had
found and discovered that it does not work for me. I'm not sure why.
| |
| inderpaul_s@yahoo.com 2007-10-30, 7:09 pm |
| On Oct 30, 11:41 am, Paul Lalli <mri...@gmail.com> wrote:
> On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
> wrote:
>
>
>
> Shouldn't you first be concerned about whether it's the most *correct*
> before you worry about efficiency?
>
>
> 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.
>
>
> 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++.
| |
| Jim Gibson 2007-10-30, 7:09 pm |
| In article <1193775898.489424.50020@q5g2000prf.googlegroups.com>,
<"inderpaul_s@yahoo.com"> wrote:
> On Oct 30, 11:41 am, Paul Lalli <mri...@gmail.com> wrote:
[good advice from Paul snipped]
[color=darkred]
>
> 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++.
>
Yes, it matters. Since this group is about the Perl language, everybody
who read your post assumed you were talking about the Perl regular
expression engine. Other regex engines will have their own
implementation and their own rules. You cannot assume that what is true
for Perl is also true for the regex engine you are using. You need to
read the documentation for the implementation you are using.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
| |
| inderpaul_s@yahoo.com 2007-10-30, 7:09 pm |
| On Oct 30, 12:39 pm, Jim Gibson <jimsgib...@gmail.com> wrote:
> In article <1193775898.489424.50...@q5g2000prf.googlegroups.com>,
>
> <"inderpau...@yahoo.com"> wrote:
>
>
> [good advice from Paul snipped]
>
>
>
>
> Yes, it matters. Since this group is about the Perl language, everybody
> who read your post assumed you were talking about the Perl regular
> expression engine. Other regex engines will have their own
> implementation and their own rules. You cannot assume that what is true
> for Perl is also true for the regex engine you are using. You need to
> read the documentation for the implementation you are using.
>
> --
> Jim Gibson
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com
My apologies everyone I am newbie to programming including Perl.
|
|
|
|
|