The other answers do not demonstrate the use of \D
to match non-digit characters. \D
is the opposite of \d
.
*
as a quantifier means zero or more and +
means one or more. Quantifiers immediately followed by ?
are made "lazy" -- effectively they try to make the shortest qualifying match, but this has a negative impact on performance and should be avoided when possible.
The ^
means the start of a line when the pattern has a m
flag.
Code: (Demo)
$text = 'sometext moretext 01 text
text sometext moretext 002
text text 1 (somemoretext)
etc';
preg_match_all('/^(\D*)(\d+)/m', $text, $matches);
var_export([
'non-digit' => $matches[1],
'digit' => $matches[2]
]);
Output:
array (
'non-digit' =>
array (
0 => 'sometext moretext ',
1 => 'text sometext moretext ',
2 => 'text text ',
),
'digit' =>
array (
0 => '01',
1 => '002',
2 => '1',
),
)
If you want to discard potential spaces at the end of the non-numeric string, add ?
to make the first group lazy and match zero or more whitespace characters without capturing. (Demo)
preg_match_all('/^(\D*?)\s*(\d+)/m', $text, $matches);