Trouble with Regualar Expression Pattern

J

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.


<TD width="22%" height=25>
<DIV align=right><FONT color=#000000><B><FONT face=Arial
size=2>Connection&nbsp;</FONT></B></FONT></DIV></TD>
<TD width="78%" height=25><FONT face=Arial size=2>DHCP
Client
Connected <INPUT type=submit value="DHCP Release"
name=release>&nbsp;<INPUT type=submit value="DHCP Renew" name=renew>
</FONT></TD></TR>
<TR>
<TD width="22%" height=25>
<DIV align=right><FONT color=#000000><B><FONT
face=Arial
size=2>IP Address&nbsp;</FONT></B></FONT></DIV></TD>
<TD width="78%" height=25><FONT face=Arial
size=2>177.33.152.123 </FONT></TD></TR>
 
G

Gunnar Hjalmarsson

jhu said:
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;
 
J

jkstill

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 = <DATA>;
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__

<TD width="22%" height=25>
<DIV align=right><FONT color=#000000><B><FONT face=Arial
size=2>Connection </FONT></B></FONT></DIV></TD>
<TD width="78%" height=25><FONT face=Arial size=2>DHCP
Client
Connected <INPUT type=submit value="DHCP Release"
<TD width="78%" height=25><FONT face=Arial
size=2>123.456.78.900 </FONT></TD></TR>
name=release> <INPUT type=submit value="DHCP Renew" name=renew>
</FONT></TD></TR>
<TR>
<TD width="22%" height=25>
<DIV align=right><FONT color=#000000><B><FONT
face=Arial
size=2>IP Address </FONT></B></FONT></DIV></TD>
<TD width="78%" height=25><FONT face=Arial
size=2>177.33.152.123 </FONT></TD></TR>
 
G

Gunnar Hjalmarsson

jkstill said:
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 = <DATA>;
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?
 
J

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.

<TD width="22%" height=25>
<DIV align=right><FONT color=#000000><B><FONT face=Arial
size=2>Connection </FONT></B></FONT></DIV></TD>
<TD width="78%" height=25><FONT face=Arial size=2>DHCP
Client
Connected <INPUT type=submit value="DHCP Release"
name=release> <INPUT type=submit value="DHCP Renew" name=renew>
</FONT></TD></TR>
<TR>
<TD width="22%" height=25>
<DIV align=right><FONT color=#000000><B><FONT
face=Arial
size=2>IP Address </FONT></B></FONT></DIV></TD>
<TD width="78%" height=25><FONT face=Arial
size=2>177.33.152.123 </FONT></TD></TR>

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

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top