Regex Help

I

inderpaul_s

I'm a newbie to perl expression more or less. What I want to do is to
use a regex pattern to find the presence of an IP address within a
file or string.

I found this example on the net for a pattern.... \b\d{1,3}\.\d{1,3}\.
\d{1,3}\.\d{1,3}\b

but reading on the \b means a word boundary but does it matter that I
want to find the pattern matching an IP address since the IP address
is made up of characters which are numeric and separated by a
decimal ?

Any further explanation of the above solution I have found would be
appreciated. Any other alternative solution would be appreciated.

Many thanks

Victor
 
S

smallpond

I'm a newbie to perl expression more or less. What I want to do is to
use a regex pattern to find the presence of an IP address within a
file or string.

I found this example on the net for a pattern.... \b\d{1,3}\.\d{1,3}\.
\d{1,3}\.\d{1,3}\b

but reading on the \b means a word boundary but does it matter that I
want to find the pattern matching an IP address since the IP address
is made up of characters which are numeric and separated by a
decimal ?

Any further explanation of the above solution I have found would be
appreciated. Any other alternative solution would be appreciated.

Many thanks

Victor


The pattern above will match "255.255.255.255"
but not match "foo255.255.255.255" or "255.255.255.255foo"
because those don't have word boundaries at both ends.
--S
 
P

Peter Makholm

but reading on the \b means a word boundary but does it matter that I
want to find the pattern matching an IP address since the IP address
is made up of characters which are numeric and separated by a
decimal ?

\b is the zero width point between something which is a word character
(\w) and something which isn't (\W). Word characters is defined as
alphabetic characters (a-z, A-Z), decimal digits (0-9) and the
underscore character (_) (and depending on locale something more).

So the four numeric parts of an IP-address counts as words in relation
to \b.

//Makholm
 
T

TonyV

I'm a newbie to perl expression more or less. What I want to do is to
use a regex pattern to find the presence of an IP address within a
file or string.

I found this example on the net for a pattern.... \b\d{1,3}\.\d{1,3}\.
\d{1,3}\.\d{1,3}\b

but reading on the \b means a word boundary but does it matter that I
want to find the pattern matching an IP address since the IP address
is made up of characters which are numeric and separated by a
decimal ?

Any further explanation of the above solution I have found would be
appreciated. Any other alternative solution would be appreciated.

Many thanks

Victor

This might be more than you're looking for, but from Regular
Expression Examples at this site: http://www.regular-expressions.info/examples.html

I found the following, which also captures the octets into groups for
use with $1, $2, $3, and $4:

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?
[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4]
[0-9]|[01]?[0-9][0-9]?)\b

If you don't need to capture the octets, you can use this:

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4]
[0-9]|[01]?[0-9][0-9]?)\b

These not only check for the right number of digits and dots, but
validates that the addresses are at least somewhat valid. (For
example, it won't match an illegal address like 64.128.249.712.)

Hope this helps,
--TV
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top