PHP 正则表达式在同一行分别匹配模式
PHP Regex matches pattern separately on the same line
我正在尝试使用 php 正则表达式解析文本。我写了一个匹配所有内容的模式。我不想要这个。它必须在同一行单独匹配目标。
模式:
/\((?P<type>\w+)((,)*(?P<params>[\d\w\:]+))((,)*(?P<comment>.*))\)/u
目标数据字符串:
lorem ipsum dolor (photo,61) test (photo,62,some text) (video,63) sit amet etc
结果 preg_match_all 带有 PREG_SET_ORDER 标志:
array (size=1)
0 =>
array (size=11)
0 => string '(photo,61) test (photo,62,some text) (video,63)' (length=47)
'type' => string 'photo' (length=5)
1 => string 'photo' (length=5)
2 => string ',61' (length=3)
3 => string ',' (length=1)
'params' => string '61' (length=2)
4 => string '61' (length=2)
5 => string ') test (photo,62,some text) (video,63' (length=37)
6 => string '' (length=0)
'comment' => string ') test (photo,62,some text) (video,63' (length=37)
7 => string ') test (photo,62,some text) (video,63' (length=37)
如果目标数据被放置在单独的行中并且相同的正则表达式模式执行结果; (我想要这些结果但没有换行)
lorem ipsum dolor
(photo,61) test
(photo,62,some text)
(video,63) sit amet etc
array (size=3)
0 =>
array (size=11)
0 => string '(photo,61)' (length=10)
'type' => string 'photo' (length=5)
1 => string 'photo' (length=5)
2 => string ',61' (length=3)
3 => string ',' (length=1)
'params' => string '61' (length=2)
4 => string '61' (length=2)
5 => string '' (length=0)
6 => string '' (length=0)
'comment' => string '' (length=0)
7 => string '' (length=0)
1 =>
array (size=11)
0 => string '(photo,62,some text)' (length=20)
'type' => string 'photo' (length=5)
1 => string 'photo' (length=5)
2 => string ',62' (length=3)
3 => string ',' (length=1)
'params' => string '62' (length=2)
4 => string '62' (length=2)
5 => string ',some text' (length=10)
6 => string ',' (length=1)
'comment' => string 'some text' (length=9)
7 => string 'some text' (length=9)
2 =>
array (size=11)
0 => string '(video,63)' (length=10)
'type' => string 'video' (length=5)
1 => string 'video' (length=5)
2 => string ',63' (length=3)
3 => string ',' (length=1)
'params' => string '63' (length=2)
4 => string '63' (length=2)
5 => string '' (length=0)
6 => string '' (length=0)
'comment' => string '' (length=0)
7 => string '' (length=0)
感谢您的帮助。
我正在尝试使用 php 正则表达式解析文本。我写了一个匹配所有内容的模式。我不想要这个。它必须在同一行单独匹配目标。
模式:
/\((?P<type>\w+)((,)*(?P<params>[\d\w\:]+))((,)*(?P<comment>.*))\)/u
目标数据字符串:
lorem ipsum dolor (photo,61) test (photo,62,some text) (video,63) sit amet etc
结果 preg_match_all 带有 PREG_SET_ORDER 标志:
array (size=1)
0 =>
array (size=11)
0 => string '(photo,61) test (photo,62,some text) (video,63)' (length=47)
'type' => string 'photo' (length=5)
1 => string 'photo' (length=5)
2 => string ',61' (length=3)
3 => string ',' (length=1)
'params' => string '61' (length=2)
4 => string '61' (length=2)
5 => string ') test (photo,62,some text) (video,63' (length=37)
6 => string '' (length=0)
'comment' => string ') test (photo,62,some text) (video,63' (length=37)
7 => string ') test (photo,62,some text) (video,63' (length=37)
如果目标数据被放置在单独的行中并且相同的正则表达式模式执行结果; (我想要这些结果但没有换行)
lorem ipsum dolor
(photo,61) test
(photo,62,some text)
(video,63) sit amet etc
array (size=3)
0 =>
array (size=11)
0 => string '(photo,61)' (length=10)
'type' => string 'photo' (length=5)
1 => string 'photo' (length=5)
2 => string ',61' (length=3)
3 => string ',' (length=1)
'params' => string '61' (length=2)
4 => string '61' (length=2)
5 => string '' (length=0)
6 => string '' (length=0)
'comment' => string '' (length=0)
7 => string '' (length=0)
1 =>
array (size=11)
0 => string '(photo,62,some text)' (length=20)
'type' => string 'photo' (length=5)
1 => string 'photo' (length=5)
2 => string ',62' (length=3)
3 => string ',' (length=1)
'params' => string '62' (length=2)
4 => string '62' (length=2)
5 => string ',some text' (length=10)
6 => string ',' (length=1)
'comment' => string 'some text' (length=9)
7 => string 'some text' (length=9)
2 =>
array (size=11)
0 => string '(video,63)' (length=10)
'type' => string 'video' (length=5)
1 => string 'video' (length=5)
2 => string ',63' (length=3)
3 => string ',' (length=1)
'params' => string '63' (length=2)
4 => string '63' (length=2)
5 => string '' (length=0)
6 => string '' (length=0)
'comment' => string '' (length=0)
7 => string '' (length=0)
感谢您的帮助。