Trouble with Regualar Expression Pattern

Trouble with Regualar Expression Pattern

am 21.11.2007 01:13:01 von jhu

I'm not sure how to piece together a pattern which will find an IP
address after a specific string ?

So for example if the web page be searched is the snippet below and
what I want is to get the IP address 177.33.152.123. But I need to
make sure that the pattern search starts after the text "DHCP Renew"
because there are other IP addresses above the "DHCP Renew" but I have
enclosed only a snippet of the returned web page.

I have the following \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} and that works
to get the first available IP but not the one I want. Thanks in
advance.



size=2>Connection 

DHCP
Client
Connected name=release> 



face=Arial
size=2>IP Address 

size=2>177.33.152.123

Re: Trouble with Regualar Expression Pattern

am 21.11.2007 01:38:17 von Petr Vileta

jhu wrote:
> I'm not sure how to piece together a pattern which will find an IP
> address after a specific string ?
>
>
>
>

> size=2>Connection 

> DHCP
> Client
> Connected > name=release> 
>

>
>
>
> face=Arial
> size=2>IP Address 

> > size=2>177.33.152.123

I should go this way (I assume sniped above is in variable $code)

if($code=~m/^.+?>ip\s+address.+?>(\d{1,3}\.\d{1,3}\.\d{1,3}\ .\d{1,3})\s*<.+$/is)
{
$ip = $1;
}

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: Trouble with Regualar Expression Pattern

am 21.11.2007 01:48:44 von Gunnar Hjalmarsson

jhu wrote:
> I'm not sure how to piece together a pattern which will find an IP
> address after a specific string ?
>
> So for example if the web page be searched is the snippet below and
> what I want is to get the IP address 177.33.152.123. But I need to
> make sure that the pattern search starts after the text "DHCP Renew"
> because there are other IP addresses above the "DHCP Renew" but I have
> enclosed only a snippet of the returned web page.
>
> I have the following \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} and that works
> to get the first available IP but not the one I want.

my ($ip) = $string =~ /DHCP Renew.+?(\d{1,3}(?:\.\d{1,3}){3})/s;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Trouble with Regualar Expression Pattern

am 21.11.2007 01:55:14 von jkstill

On Nov 20, 4:13 pm, jhu wrote:
> So for example if the web page be searched is the snippet below and
> what I want is to get the IP address 177.33.152.123. But I need to
> make sure that the pattern search starts after the text "DHCP Renew"
> because there are other IP addresses above the "DHCP Renew" but I have
> enclosed only a snippet of the returned web page.
>

Though not perfect, this may get you started.
Note that I included the '\D' preceding the IP address to detect the
last character (non-digit) preceding the pattern. Not doing so
results in getting only the last digit of the first octet.

---------------------------
my @t = ;
my $t = join(' ',@t);
print $t;

$t =~ s/^.*(DHCP Renew).*\D{1}(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*$/
$2/s;

print "t: $t\n";

__DATA__


size=2>Connection

DHCP
Client
Connected size=2>123.456.78.900
name=release>



face=Arial
size=2>IP Address

size=2>177.33.152.123

Re: Trouble with Regualar Expression Pattern

am 21.11.2007 02:03:42 von Gunnar Hjalmarsson

jkstill wrote:
> On Nov 20, 4:13 pm, jhu wrote:
>> So for example if the web page be searched is the snippet below and
>> what I want is to get the IP address 177.33.152.123. But I need to
>> make sure that the pattern search starts after the text "DHCP Renew"
>> because there are other IP addresses above the "DHCP Renew" but I have
>> enclosed only a snippet of the returned web page.
>
> Though not perfect, this may get you started.
> Note that I included the '\D' preceding the IP address to detect the
> last character (non-digit) preceding the pattern. Not doing so
> results in getting only the last digit of the first octet.
>
> ---------------------------
> my @t = ;
> my $t = join(' ',@t);
> print $t;
>
> $t =~ s/^.*(DHCP Renew).*\D{1}(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*$/
> $2/s;

Why use the s/// operator for just extracting something?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Trouble with Regualar Expression Pattern

am 21.11.2007 02:13:10 von jhu

On Nov 20, 4:13 pm, jhu wrote:
> I'm not sure how to piece together a pattern which will find an IP
> address after a specific string ?
>
> So for example if the web page be searched is the snippet below and
> what I want is to get the IP address 177.33.152.123. But I need to
> make sure that the pattern search starts after the text "DHCP Renew"
> because there are other IP addresses above the "DHCP Renew" but I have
> enclosed only a snippet of the returned web page.
>
> I have the following \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} and that works
> to get the first available IP but not the one I want. Thanks in
> advance.
>
>
>

> size=2>Connection

> DHCP
> Client
> Connected > name=release>
>

>
>
>
> face=Arial
> size=2>IP Address

> > size=2>177.33.152.123

Many thanks everyone. I'm going to work with the ones everyone has
given and go from there.