Regex Help

Regex Help

am 29.10.2007 19:14:44 von 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

Re: Regex Help

am 29.10.2007 19:58:49 von smallpond

On Oct 29, 2:14 pm, "inderpau...@yahoo.com"
wrote:
> 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

Re: Regex Help

am 29.10.2007 20:09:26 von Peter Makholm

"inderpaul_s@yahoo.com" writes:

> 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

Re: Regex Help

am 29.10.2007 20:12:38 von kingskippus

On Oct 29, 2:14 pm, "inderpau...@yahoo.com"
wrote:
> 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

Re: Regex Help

am 29.10.2007 21:51:19 von rvtol+news

inderpaul_s@yahoo.com schreef:

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

Consider Regexp::Common, specifically $RE{net}{ipv4}.
http://search.cpan.org/~abigail/Regexp-Common/lib/Regexp/Com mon/net.pm

--
Affijn, Ruud

"Gewoon is een tijger."

Re: Regex Help

am 30.10.2007 09:40:02 von usenet

On Oct 29, 10:14 am, "inderpau...@yahoo.com"
wrote:
> What I want to do is to use a regex pattern to find the presence of an IP address

That wheel has already been invented:

http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Rege xp/Common/net.pm


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Re: Regex Help

am 30.10.2007 10:48:57 von merl the perl

wrote in message
news:1193683097.115702.174840@v23g2000prn.googlegroups.com.. .
> On Oct 29, 10:14 am, "inderpau...@yahoo.com"
> wrote:
>> What I want to do is to use a regex pattern to find the presence of an IP
>> address
>
> That wheel has already been invented:
>
>
> http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Rege xp/Common/net.pm
>
How do you install this wheel?

--
wade ward

President
Merrill Jensen Consulting


wade@zaxfuuq.net
435 -838-7760

Re: Regex Help

am 30.10.2007 11:48:04 von Tad McClellan

Wade Ward wrote:
>
> wrote in message
> news:1193683097.115702.174840@v23g2000prn.googlegroups.com.. .
>> On Oct 29, 10:14 am, "inderpau...@yahoo.com"
>> wrote:
>>> What I want to do is to use a regex pattern to find the presence of an IP
>>> address
>>
>> That wheel has already been invented:
>>
>>
>> http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Rege xp/Common/net.pm
>>
> How do you install this wheel?


perldoc -q install

How do I install a module from CPAN?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Regex Help

am 30.10.2007 22:02:13 von rvtol+news

Dr.Ruud schreef:
> 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.
>
> Consider Regexp::Common, specifically $RE{net}{ipv4}.
> http://search.cpan.org/~abigail/Regexp-Common/lib/Regexp/Com mon/net.pm

s/ipv4/IPv4/

--
Affijn, Ruud

"Gewoon is een tijger."

Re: Regex Help

am 31.10.2007 04:25:25 von merl the perl

"Tad McClellan" wrote in message
news:slrnfie2dl.hvc.tadmc@tadmc30.sbcglobal.net...
> Wade Ward wrote:
>>
>> wrote in message
>> news:1193683097.115702.174840@v23g2000prn.googlegroups.com.. .
>>> On Oct 29, 10:14 am, "inderpau...@yahoo.com"
>>> wrote:
>>>> What I want to do is to use a regex pattern to find the presence of an
>>>> IP
>>>> address
>>>
>>> That wheel has already been invented:
>>>
>>>
>>> http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Rege xp/Common/net.pm
>>>
>> How do you install this wheel?
>
>
> perldoc -q install
>
> How do I install a module from CPAN?
In other languages, the imperative form of the verb also indicates the
gender of the target.
--
wade ward

President
Merrill Jensen Consulting


wade@zaxfuuq.net
435 -838-7760