Make this regex more efficient?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Make this regex more efficient?

Post by pickle »

Hi all,

I've successfully built a regular expression, but I'm quite the neophyte when it comes to these things. I was wondering if there was a more efficient pattern that could be employed here.

pattern: "/([0-9]{2,3}.{1}[0-9]{3}.{1}[0-9]{2,3}.{1}[0-9]{1,3}).*[0-9]+.*[0-9]+[^0-9]+([0-9]+)/"
target: "10.131.255.255 7625 0 11928"

I want to get the ip and the last number (in this case 11928).

I know that this pattern won't match ALL ips, but the ips I'm going to be dealing with are very regular, and will fit into this pattern.

Any ideas? Thanks.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The pattern is wrong anyway... didnt escape your dots :P

Code: Select all

/^(\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*? (\d+)$/
The IP makes it look long but it's needed unfortunately ;)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

That pattern you gave me returns empty matches. I had trouble with the \d not evaluating. Neither does \D or \s. Is there a php.ini thing I need to change to enable them?

I know I didn't escape the dots - it worked without me doing it. Using \. didn't match either - I had to use .{1} like I had.

It sounds like my system is very goofy when evaluating regular expressions.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Nothing in php.ini, regex are pretty standard. Just double check you're using it correctly. If it's still not working post some code and I'll have a look ;)

Code: Select all

$target = "10.131.255.255 7625 0 11928";
preg_match('/^(\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*? (\d+)$/', $target, $matches);
print_r($matches);

Code: Select all

Array
(
    ї0] => 10.131.255.255 7625 0 11928
    ї1] => 10.131.255.255
    ї2] => 11928
)
EDIT | If you define the pattern outside the preg_match() you need to double-up your backslashes :D
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

As far as I know IPs can be of type 1.1.1.1 (no requirement on 2 digits in the first field).

I believe IBM or some other big shot companies have some IPs in the 9 first positions.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

$target = "10.131.255.255 7625 0 11928";
$matches = sscanf($target, "%d.%d.%d.%d %d %d %d");
print_r($matches);
Post Reply