...

View Full Version : Regex digit plus sign digit



mathceleb
11-03-2012, 12:42 AM
The variable "add_count" is always 1. Here is what I tried:


$n="6+0+1";
$add_count = preg_match_all('/-?\d+(\+)\d+/', $n, $match2);

Also tried this:


$n="6+0+1";
$add_count = preg_match_all('/\b-?\d+(\+)\d+$/', $n, $match2);

It still evaluates to 1 and not 2.

felgall
11-03-2012, 02:47 AM
The first expression matches to 6+0 and then decides that +1 doesn't match.

The second one matches 0+1 and then doesn't have any further content to check.

So on both cases there is only one number+number that can be matched (given that the search for another match starts from the end of the previously matched text).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum