常规exp =(数字)*(A | B | DF | XY)+(数字)+
我真的很困惑这种模式我想用PHP分隔此字符串,有人可以帮我我的输入可能是这样的
并转换为此
Array
(
[0] => 12
[1] => XY
[2] => 1234567890
)
<?php
$input = "12 XY 123456789";
print_r(preg_split('/\d*[(A|B|DF|XY)+\d+]+/', $input, 3));
//print_r(preg_split('/[\s,]+/', $input, 3));
//print_r(preg_split('/\d*[\s,](A|B)+[\s,]\d+/', $input, 3));
您可以匹配并捕获数字,字母和数字:
$input = "12 XY 123456789";
if (preg_match('/^(?:(\d+)\s*)?(A|B|DF|XY)(?:\s*(\d+))?$/', $input, $matches)){
array_shift($matches);
print_r($matches);
}
请参见PHP demo和regex demo。