?
Solved

Regex to find IP address and port number

Posted on 2010-01-12
7
Medium Priority
?
6,634 Views
Last Modified: 2012-05-08
This regex returns IP address:

/\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/

However I'd like to combine it with the below so that I can return IP addresses AND ports.  The regex for ports is something like this:

^(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)$

So I want to run the below code and instead of having it return just 193.143.121.217 and 129.10.120.193 like it does today, I'd like it to return something like:

193.143.121.217:3128
129.10.120.193:3124

Thanks!

<?php
$ip_address_pattern='/\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/';
preg_match_all($ip_address_pattern, '<tr><td>193.143.121.217:3128</td><td>my stuff</td><td>6 33 asdfWE#R3452</td><td>more 129.10.120.193:3124 stuff 23</td>', $matches, PREG_SET_ORDER);

print_r($matches);
?>

Open in new window

0
Comment
Question by:Geoff Millikan
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 32

Expert Comment

by:DrDamnit
ID: 26300669
Here you go:
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}

Open in new window

0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26300670
This will find the IP address
str = "http://127.0.0.0:9090/css/pr_list-"
var myArray = str.match(/\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/g)

myArray[0] is 127.0.0.0
0
 
LVL 32

Expert Comment

by:DrDamnit
ID: 26300677
That regex will also capture invaild IP addresses; however, it is highly unlikely that your source that you'll be comparing it to will generate invalid IP addresses and ports in that pattern.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 1400 total points
ID: 26300687
var myArray = str.match(/\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]?):[0-9]{1,5}/g)

will match both the ip and port number
0
 
LVL 15

Assisted Solution

by:Insoftservice
Insoftservice earned 600 total points
ID: 26300689
0
 

Author Closing Comment

by:Geoff Millikan
ID: 31676516
Thanks!
0
 

Author Comment

by:Geoff Millikan
ID: 26310199
Ended up using a combo of the two answers:
<?php
$ip_address_and_port_pattern='/\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]?)\:[0-9]{1,5}\b/';
preg_match_all($ip_address_pattern, $test_string_with_ip_addresses, $matches, PREG_SET_ORDER);
print_r($matches);
?>

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question