常規 exp = (Digits)*(A|B|DF|XY)+(Digits)+我對這種模式感到困惑真的我想在 PHP 中分隔這個字符串,有人可以幫助我我的輸入可能是這樣的A1234乙12391A12312A1231A 123412 一個 1231234乙 12345678912 XY 1234567890并轉換成這個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));
1 回答

紅顏莎娜
TA貢獻1842條經驗 獲得超13個贊
您可以匹配并捕獲數字、字母和數字:
$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 組:任何或更多數字
$
- 字符串結束。
- 1 回答
- 0 關注
- 162 瀏覽
添加回答
舉報
0/150
提交
取消