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.
Make this regex more efficient?
Moderator: General Moderators
Make this regex more efficient?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
The pattern is wrong anyway... didnt escape your dots 
The IP makes it look long but it's needed unfortunately 
Code: Select all
/^(\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*? (\d+)$/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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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 
EDIT | If you define the pattern outside the preg_match() you need to double-up your backslashes 
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
)Code: Select all
$target = "10.131.255.255 7625 0 11928";
$matches = sscanf($target, "%d.%d.%d.%d %d %d %d");
print_r($matches);