?
Solved

Regex to find IP address and port number

Posted on 2010-01-12
7
Medium Priority
?
6,122 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:gurvinder372
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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 40

Accepted Solution

by:
gurvinder372 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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

801 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