Possible Duplicate:
What do ^ and $ mean in a regular expression?
I'm not good at all at regular expression, i know they are so much important but never found the time to study them. Anyway i'm just tring to ensure that, among these strings:
MAX_AGE_60
MAX_AGE_80
...
MAX_AGE_X
where more in general X
is a integer
, at least one match is found and get the number X
. So following the example on PHP documentation i wrote this:
// Starts and ends with the literal 'MAX_AGE_' followed by a group that is a
// number with at least one digit. Case insensitive
$pattern = '/^MAX_AGE_(?P<max>\d+)$/i';
$test = 'MAX_AGE_80';
$matches = array();
preg_match($pattern, $test, $matches);
var_dump($matches);
The result is wrong (suprise...):
array (size=0)
empty
Removing start/end delimiters (^
and $
) i get the correct result:
array (size=3)
0 => string 'MAX_AGE_80' (length=10)
'max' => string '80' (length=2)
1 => string '80' (length=2)
So why i can't force that the string must start and end with that pattern? And how should interpret the result array? I mean indexes 0
, 1
and max
?