Bot*_*tyZ 2 php regex preg-match
我正在尝试从字符串中提取序列号以进行匹配比较,并认为使用preg_match可能是可行的,但是我在正则表达式方面苦苦挣扎。
有人可以提供任何帮助吗?
当前尝试如下:
$example = "CPM-200:0123456L|CPM-100:9876543L|CJ Pro:CJP33-011";
pre_match("/\:(.*?)\|/", $example, $matches);
var_dump($matches);
Run Code Online (Sandbox Code Playgroud)
目前,以上内容吐出:
Array(2) {
[0]=> string(10) ":0123456L|"
[1]=> string(8) "0123456L"
}
Run Code Online (Sandbox Code Playgroud)
但我实际上想将其提取为:
Array(0) {
[0] => 0123456L
[1] => 9876543L
[2] => CJP33-011
}
Run Code Online (Sandbox Code Playgroud)
我从未对regex感到满意!我尝试了各种组合,而以上是我设法达到的最接近的组合。需要找到一个像样的在线教程。
你可以用这个
:([^|]+)(?:\||$)
Run Code Online (Sandbox Code Playgroud)
正则表达式分解
: #Match :
([^|]+) #Match anything other than |
(?:
\| #Match | literally
| #Alternation(OR) --> Match | or $
$ #End of string
)
Run Code Online (Sandbox Code Playgroud)
PHP代码
$re = "/:([^|]+)(?:\\||$)/";
$str = "CPM-200:0123456L|CPM-100:9876543L|CJ Pro:CJP33-011";
preg_match_all($re, $str, $matches);
print_r($matches[1]);
Run Code Online (Sandbox Code Playgroud)