Home > front end >  Regex to match first '15' in a sequence of numbers
Regex to match first '15' in a sequence of numbers

Time:10-19

i need a regex that only matches the first '15'(number fifteen) ocurrence that appeared in a string. Im using php.

Example: 294515631576 (the first '15' appears after 2945).

I tried with this regex /15(?=)/ but this match the '15' between 63 and 76.

CodePudding user response:

Try this regex:

^(\d*?)15(\d*)


$re = '/^(\d*?)15(\d*)/';
$str = '294515631576 ';

preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0);

// Print the entire match result
var_dump($matches);

OUTPUT:

GROUP1 0-4      2945
GROUP2 6-12     631576

CodePudding user response:

you can use (15) grouping, then access the first matched group

  • Related