根据字母后跟数字拆分字符串

Splitting the string based on alphabet followed by numbers

我尝试拆分字符串 A1B22C333 以获得数组 [0] => A1[1] => B22[2] => C333。这是我失败的尝试:

$mystring = "A1B22C333";
$pattern  = "[\w\d+]";
$arr = preg_split("/".$pattern."/", $mystring);
print_r($arr);

但我得到: Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => )

而不是 preg_split 使用 preg_match_all 以及以下正则表达式

([a-zA-Z]\d+)

所以你的代码看起来像

preg_match_all("/([a-zA-Z]\d+)/","A1B22C333",$m);
print_r($m[0]);

输出:

Array
(
    [0] => A1
    [1] => B22
    [2] => C333
)

您可以使用像

这样的正则表达式
(?<!^)(?=\D)

regex demo

(?<!^)(?=\D) 匹配不在字符串开头和非数字字符之前的位置。

参见IDEONE demo

$mystring = "A1B22C333";
$pattern  = '~(?<!^)(?=\D)~';
$arr = preg_split($pattern, $mystring);
print_r($arr);
// => Array( [0] => A1    [1] => B22    [2] => C333 )

如果您使用 PREG_SPLIT_NO_EMPTY (demo):

,您也可以将正则表达式缩短为 '~(?=\D)~'(非数字之前的位置)
$pattern  = '~(?=\D)~';
$arr = preg_split($pattern, $mystring, -1, PREG_SPLIT_NO_EMPTY);

我认为你应该使用 preg_match_all。

preg_match_all("/([A-Z]\d+)/", "A1B22C333", $output_array);

点击preg_match_all
http://www.phpliveregex.com/p/fnj