Osa
Osa

Reputation: 1990

RegEx use one for both cases?

Okay well, here's the string

$string = "123456789_some_0.png";

i currently use preg_match to get the " 123456789 " using the following pattern :

$pattern = "/[0-9]*/i";

well, there are 2 formats for the string, i want to come up with the same result for this case :

$string = "1234-123456789_some_0.png";

and to come up with " 12345789 " and only from both cases, how to do it ?

Upvotes: 0

Views: 49

Answers (1)

João Silva
João Silva

Reputation: 91299

Assuming you want to capture all digits that are followed by an underscore, you could use the following:

$strings = array("1234-123456789_some_0.png", "123456789_some_0.png");
foreach ($strings as $string) {
  preg_match("/([0-9]+)_/", $string , $matches);
  echo $matches[1], PHP_EOL; // 123456789
}

DEMO.

Upvotes: 2

Related Questions