user1475463
user1475463

Reputation: 15

Pattern Match help on http response

I've got the following from an http get. I don't understand why my pattern match isn't catching the ip address.

                                    <div style="text-align:center;font-  size:26px;padding-top:0px;color:#000;">Your IP Address Is:</div>
                                    <div style="text-align:center;font-size:26px;padding-top:10px;font-weight:bold;color:#007cc3;">
<!-- do not script -->
144.160.5.25
<!-- do not script -->

My pattern match is

$res->content =~ /Your IP Address Is:.*((?:\d{1,3}\.){3}\d{1,3})/) {

If I do this, then it does find it.

$res->content =~ /Your IP Address Is:\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s\S+\s+((?:\d{1,3}\.){3}\d{1,3})/) {

Upvotes: 0

Views: 45

Answers (1)

Miller
Miller

Reputation: 35198

Your problem lies in the fact that . won't match a return character unless you use the /s switch.

Additionally, you should probably use non-greedy .*? versus greedy .* matching.

$res->content =~ /Your IP Address Is:.*?((?:\d{1,3}\.){3}\d{1,3})/s

Finally, if the webpage states <!-- do not script --> around this ip address, you probably aren't following the Terms of Service of this website. Look into if there is an API already provided by this website for accessing this data.

Upvotes: 3

Related Questions