Nic*_*ung 3 php regex preg-match preg-split
我的字符串是$text1 = 'A373R12345'
我想要查找此字符串的最后一个数字编号.
所以我使用这个正则表达式^(.*)[^0-9]([^-]*)
然后我得到了这个结果:
1.A373
2.12345
但我的预期结果是:
1.A373R
(它有'R')
2.12345
另一个例子是$text1 = 'A373R+12345'
然后我得到了这个结果:
1.A373R
2.12345
但我的预期结果是:
1.A373R +
(它有'+')
2.12345
我想要包含最后一个没有数字的数字!!
请帮忙 !!谢谢!!
$text1 = 'A373R12345';
preg_match('/^(.*[^\d])(\d+)$/', $text1, $match);
echo $match[1]; // A373R
echo $match[2]; // 12345
$text1 = 'A373R+12345';
preg_match('/^(.*[^\d])(\d+)$/', $text1, $match);
echo $match[1]; // A373R+
echo $match[2]; // 12345
Run Code Online (Sandbox Code Playgroud)
解析正则表达式:
^ match from start of string
(.*[^\d]) match any amount of characters where the last character is not a digit
(\d+)$ match any digit character until end of string
Run Code Online (Sandbox Code Playgroud)
