正则表达式在方括号内找到字符串[重复]

时间:2022-12-01 21:43:27

This question already has an answer here:

这个问题在这里已有答案:

I need to create three regular expressions to extract the multiple words within square brackets , using the preg_match_all ()

我需要使用preg_match_all()创建三个正则表达式来提取方括号内的多个单词

example

Input string:

输入字符串:

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.

Lorem [ipsum] dolor坐[[amet]],nam你dolores detracto definitionem。在这里,et admodum fabellas patrioque,在[[summo]] alienum cotidieque的背景下顽固的海。 Quo tollit dolores cu viderer [Theophrastus] to vix。暨家庭的assibver,assant vocent democritum eum ut ipsum nec timeam and Persius。 Putant suscipit abhorreant jus cu。 Pri doctus feugiat facilisis not。 [[[东]]]在sint malis和Equidem骚扰inciderint海,[[[vocibus]]] id mentitum海。

1 . desired output for single brackets

1。单个括号的所需输出

[0 ] => Array
        (
           [0 ] => ipsum
           [1 ] => Theophrastus
       )

2 . desired output for double square brackets

2。双方括号的所需输出

[0 ] => Array
       (
           [0 ] => amet
           [1 ] => summo
       )

3 . desired output for triple square brackets

3。三方括号的所需输出

[0 ] => Array
       (
           [0 ] = > East
           [1 ] => vocibus
       )

Appreciate any help !

感谢任何帮助!

3 个解决方案

#1


1  

Try following:

试试以下:

preg_match_all("/\[(\s*[\w]*\s*)\]/", $input, $output, PREG_PATTERN_ORDER);

Demo

演示

#2


1  

UPDATE:

更新:

for 1

为1

preg_match_all('/[^\[]\[([^\[].*)\]/U', $text, $matches)

for 2

2

preg_match_all('/[^\[]\[\s?\[([^\[].*)\]/U', $text, $matches)

for 3

为3

preg_match_all('/[^\[]\[{3}([^\[].*)\]/U', $text, $matches)

demo

演示

#3


0  

This not-so-readable looking regex should work:

这个看起来不那么可读的正则表达式应该可以工作:

$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]);

OUTPUT:

Array
(
    [0] =>  ipsum
    [1] =>  Theophrastus
)
Array
(
    [0] =>  amet
    [1] =>  summo
)
Array
(
    [0] =>  East
    [1] =>  vocibus
)

#1


1  

Try following:

试试以下:

preg_match_all("/\[(\s*[\w]*\s*)\]/", $input, $output, PREG_PATTERN_ORDER);

Demo

演示

#2


1  

UPDATE:

更新:

for 1

为1

preg_match_all('/[^\[]\[([^\[].*)\]/U', $text, $matches)

for 2

2

preg_match_all('/[^\[]\[\s?\[([^\[].*)\]/U', $text, $matches)

for 3

为3

preg_match_all('/[^\[]\[{3}([^\[].*)\]/U', $text, $matches)

demo

演示

#3


0  

This not-so-readable looking regex should work:

这个看起来不那么可读的正则表达式应该可以工作:

$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]);

OUTPUT:

Array
(
    [0] =>  ipsum
    [1] =>  Theophrastus
)
Array
(
    [0] =>  amet
    [1] =>  summo
)
Array
(
    [0] =>  East
    [1] =>  vocibus
)