PHP 中的正则表达式。获取第一级括号之间的文本

RegExp in PHP. Get text between first level parentheses

我在一篇文章中有两种类型的字符串:

a(bc)de(fg)h

a(bcd(ef)g)h

我需要在一级括号之间获取文本。在我的示例中,这是:

bc

fg

bcd(ef)g

我尝试将下一个正则表达式 /\((.+)\)/ 与 Ungreedy (U) 标志一起使用:

bc

fg

bcd(ef

没有它:

bc)de(fg

bcd(ef)g

两种变体都不符合我的需要。也许有人知道如何解决我的问题?

请你试试看:

preg_match("/\((.+)\)/", $input_line, $output_array);

http://www.phpliveregex.com/

中测试此代码
Regex: \((.+)\)
Input: a(bcd(eaerga(er)gaergf)g)h
Output: array(2
   0    =>  (bcd(eaerga(er)gaergf)g)
   1    =>  bcd(eaerga(er)gaergf)g
)

这个 question 几乎有答案,但实现有点模棱两可。您可以在没有 ~ 的情况下使用接受的答案中的逻辑来获取此正则表达式:

\(((?:\[^\(\)\]++|(?R))*)\)

Tested 输出为:

使用 PCRE Recursive pattern 匹配嵌套括号中的子字符串:

$str = "a(bc)de(fg)h some text a(bcd(ef)g)h ";
preg_match_all("/\((((?>[^()]+)|(?R))*)\)/", $str, $m);

print_r($m[1]);

输出:

Array
(
    [0] => bc
    [1] => fg
    [2] => bcd(ef)g
)

\( ( (?>[^()]+) | (?R) )* \)

First it matches an opening parenthesis. Then it matches any number of substrings which can either be a sequence of non-parentheses, or a recursive match of the pattern itself (i.e. a correctly parenthesized substring). Finally, there is a closing parenthesis.


技术注意事项:

If there are more than 15 capturing parentheses in a pattern, PCRE has to obtain extra memory to store data during a recursion, which it does by using pcre_malloc, freeing it via pcre_free afterwards. If no memory can be obtained, it saves data for the first 15 capturing parentheses only, as there is no way to give an out-of-memory error from within a recursion.