PHP,我怎么能;摘录;方括号之间的所有子字符串?

PHP,我怎么能;摘录;方括号之间的所有子字符串?,php,regex,Php,Regex,假设我有一个字符串,比如: "there are some words, [other, could, be, here] then other words [and,more,here]." 或 我想要一个括号组数组,类似于: Array ( [0] => [other, could, be, here] [1] => [and,more,here] ) 或者我以后可以用的任何东西,一个物体也应该可以。 事实上,我已经尝试了一些正则表达式,但没有运气,似乎我不能接受多个组,或者

假设我有一个字符串,比如:

"there are some words, [other, could, be, here] then other words [and,more,here]."

我想要一个括号组数组,类似于:

Array ( [0] => [other, could, be, here] [1] => [and,more,here] )
或者我以后可以用的任何东西,一个物体也应该可以。 事实上,我已经尝试了一些正则表达式,但没有运气,似乎我不能接受多个组,或者如果字符串以匹配开头,它就会出现问题

已用于此操作的正则表达式包括:

'/(\[\S+)\s(\S+\])/i'
'/\[([^]]+)\]/i'
'/\[(.*?)\]/i'
我做错了什么?

试试这个正则表达式:

$str= "[one,two,three] something outside [than, more here, and more].";

preg_match_all("/\[[^\]]*\]/", $str, $matches);
var_dump($matches[0]);
试试这个正则表达式:

$str= "[one,two,three] something outside [than, more here, and more].";

preg_match_all("/\[[^\]]*\]/", $str, $matches);
var_dump($matches[0]);
期望“非零”介于
[]

preg\u match\u all('\\[([^\]+)\]\\\\]\\\\\\\\\\\\\\\\\\\\”,$string,$matches\u数组)

print\u r($matches\u数组)

或者

[]

preg\u match\u all('\\[([^\]*?)\]\\\\\\\\\\\',$string,$matches\u数组)

还有。。。将后向引用括号
()
放在要捕获的计数周围(是否真的要捕获括号?

希望“不是空的”介于
[]

preg\u match\u all('\\[([^\]+)\]\\\\]\\\\\\\\\\\\\\\\\\\\”,$string,$matches\u数组)

print\u r($matches\u数组)

或者

[]

preg\u match\u all('\\[([^\]*?)\]\\\\\\\\\\\',$string,$matches\u数组)


还有。。。在要捕获的计数周围放置反向引用括号(是否确实要捕获括号?

如果必须使用正则表达式,则忽略此答案,但这里有另一种方法:

$str="there are some words, [other, could, be, here] then other words 
[and,more,here].";
$found=array();
$foundcount=0;
for($i=0;$i<strlen($str);$i++){
    if($str[$i]=='['){
        $found[$foundcount]="";
        for($j=$i;$j<strlen($str);$j++,$i++){
            $found[$foundcount].=$str[$j];
            if($str[$j]==']'){
                $foundcount++;
                break;
            }
        }
    }
}
$str=“有些词,[其他的,可能的,在这里]然后是其他词
[这里还有更多]”;
$found=array();
$foundcount=0;

对于($i=0;$i如果必须使用正则表达式,则忽略此答案,但这里有另一种方法:

$str="there are some words, [other, could, be, here] then other words 
[and,more,here].";
$found=array();
$foundcount=0;
for($i=0;$i<strlen($str);$i++){
    if($str[$i]=='['){
        $found[$foundcount]="";
        for($j=$i;$j<strlen($str);$j++,$i++){
            $found[$foundcount].=$str[$j];
            if($str[$j]==']'){
                $foundcount++;
                break;
            }
        }
    }
}
$str=“有些词,[其他的,可能的,在这里]然后是其他词
[这里还有更多]”;
$found=array();
$foundcount=0;

对于($i=0;$i如果要保留方括号,并且必须有子字符串,可以这样做:

解释

  • 匹配方括号
    \[
  • 不匹配方括号一次或多次(如果必须有子字符串,或者使用
    *
    而不是
    +
    来匹配空括号[])
    [^[]+
  • 匹配结束方括号
    \]
例如:

$re = '~\[[^[]+\]~';
$str = "there are some words, [other, could, be, here] then other words [and,more,here].";
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
$result = [];

foreach ($matches as $match) {
    $result[] = $match[0];
}

var_dump($result);

如果要在方括号内匹配,可以使用


如果要保留方括号并且必须有子字符串,可以这样做:

解释

  • 匹配方括号
    \[
  • 不匹配方括号一次或多次(如果必须有子字符串,或者使用
    *
    而不是
    +
    来匹配空括号[])
    [^[]+
  • 匹配结束方括号
    \]
例如:

$re = '~\[[^[]+\]~';
$str = "there are some words, [other, could, be, here] then other words [and,more,here].";
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
$result = [];

foreach ($matches as $match) {
    $result[] = $match[0];
}

var_dump($result);

如果要在方括号内匹配,可以使用

尝试以下操作:

/\[[^\]]*\]/
例如:

$str = "there are some words, [other, could, be, here] then other words [and,more,here].";

preg_match_all('/\[[^\]]*\]/', $str, $matches);
$matches = $matches[0];
print_r($matches);
产出:

Array ( [0] => [other, could, be, here] [1] => [and,more,here] )
请尝试以下操作:

/\[[^\]]*\]/
例如:

$str = "there are some words, [other, could, be, here] then other words [and,more,here].";

preg_match_all('/\[[^\]]*\]/', $str, $matches);
$matches = $matches[0];
print_r($matches);
产出:

Array ( [0] => [other, could, be, here] [1] => [and,more,here] )

如果不使用通配符,请尝试
\[[^]*\]
如果不使用通配符,请尝试
\[^]*\]
如果不使用通配符,请尝试
\[^]*\]
如果不使用通配符,请尝试
\[^]*\]
如果不使用通配符,请尝试
如果不使用通配符,请尝试
,这意味着它可以继续匹配上次出现的
]