PHP字符串拆分规则
正则表达式=(位数)*(A | B | DF | XY)+(位数)+ 我真的对这种模式感到困惑 我想在PHP中分离这个字符串,有人可以帮助我 我的意见可能是这样的PHP字符串拆分规则,php,regex,string,split,preg-split,Php,Regex,String,Split,Preg Split,正则表达式=(位数)*(A | B | DF | XY)+(位数)+ 我真的对这种模式感到困惑 我想在PHP中分离这个字符串,有人可以帮助我 我的意见可能是这样的 A1234 B 1239 1A123 12A123 1A 1234 12 A 123 1234 B 123456789 12 XY 1234567890 然后转换成这个 Array ( [0] => 12 [1] => XY [2] => 1234567890 ) <?php $inp
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));
数组
(
[0] => 12
[1] =>XY
[2] => 1234567890
)
您可以匹配并捕获数字、字母和数字:
$input = "12 XY 123456789";
if (preg_match('/^(?:(\d+)\s*)?(A|B|DF|XY)(?:\s*(\d+))?$/', $input, $matches)){
array_shift($matches);
print_r($matches);
}
请参阅和
^
-字符串的开头
(?:(\d+)\s*)?
-可选序列:
(\d+)
-第1组:任意或多个数字
\s*
-0+空格
(A | B | DF | XY)
-第2组:A
,B
,DF
或XY
(?:\s*(\d+))
-以下可选序列:
\s*
-0+空格
(\d+)
-第3组:任意或多个数字
$
-字符串结束