我需要使用preg_match_all()
创建三个正则表达式来提取方括号内的多个单词例如
输入字符串:
Lorem [ipsum] dolor坐[[amet]],nam你dolores detracto definitionem。在这里,et admodum fabellas patrioque,在[[summo]] alienum cotidieque中,叛逆海。 Quo tollit dolores cu viderer [Theophrastus] to vix。暨家庭的assibver,putant vocent democritum eum ut ipsum nec timeam and Persius。 Putant suscipit abhorreant jus cu。 Pri doctus feugiat facilisis not。 [[[东]]]在sint malis,以及Equidem骚扰inciderint sea,[[[vocibus]]] id mentitum sea。
1。单括号的所需输出
[0 ] => Array
(
[0 ] => ipsum
[1 ] => Theophrastus
)
2。双方括号的所需输出
[0 ] => Array
(
[0 ] => amet
[1 ] => summo
)
3。三方括号的期望输出
[0 ] => Array
(
[0 ] = > East
[1 ] => vocibus
)
感谢任何帮助!
答案 0 :(得分:1)
答案 1 :(得分:1)
更新:
for 1
preg_match_all('/[^\[]\[([^\[].*)\]/U', $text, $matches)
for 2
preg_match_all('/[^\[]\[\s?\[([^\[].*)\]/U', $text, $matches)
for 3
preg_match_all('/[^\[]\[{3}([^\[].*)\]/U', $text, $matches)
答案 2 :(得分:0)
这个看起来不那么可读的正则表达式应该可以工作:
$s = 'Lorem [ ipsum ] dolor sit [[ amet ]] , nam you dolores detracto definitionem . Et admodum fabellas patrioque here , offendit an obstinate sea , vis at [[ summo ]] alienum cotidieque . Quo tollit dolores cu viderer [ Theophrastus ] to vix . Cum homes erroribus to assueverit , putant vocent democritum eum ut ipsum nec timeam and Persius . Putant suscipit abhorreant jus cu . Pri doctus feugiat facilisis not. [[[ East ]]] in sint malis , and Equidem harassment inciderint sea , [[[ vocibus ]]] id mentitum sea .';
if (preg_match_all('/(?<!\[)\[(?!\[)([^]]*)\](?!\[)/', $s, $m)) // single [...]
print_r($m[1]);
if (preg_match_all('/(?<!\[)\[\[(?!\[)([^]]*)\]\](?!\[)/', $s, $m)) // double [[...]]
print_r($m[1]);
if (preg_match_all('/(?<!\[)\[\[\[(?!\[)([^]]*)\]\]\](?!\[)/', $s, $m)) // triple [[[...]]]
print_r($m[1]);
Array
(
[0] => ipsum
[1] => Theophrastus
)
Array
(
[0] => amet
[1] => summo
)
Array
(
[0] => East
[1] => vocibus
)