Search code examples
phpregexpreg-matchtext-extraction

Get number between two characters


I am trying to find the number between two underscores (_) in strings like these:

234534_45_92374
3433_9458_034857
zx_8458_047346daf

What would be the regex for this?


Solution

  • preg_match('/_(\d+)_/', $str, $matches);
    $number = (int) $matches[1];