Regular Expression and Useage

I

inderpaul_s

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
 
T

TonyV

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
 
P

Paul Lalli

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
 
I

inderpaul_s

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.
 
I

inderpaul_s

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.
 
I

inderpaul_s

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++.
 
I

inderpaul_s

[good advice from Paul snipped]


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top