dougou8639 2017-04-08 13:10
浏览 22
已采纳

PHP中的RegExp。 在第一级括号之间获取文本

I have two type of strings in one text:

a(bc)de(fg)h

a(bcd(ef)g)h

I need to get text between first level parentheses. In my example this is:

bc

fg

bcd(ef)g

I tried to use next regular expression /\((.+)\)/ with Ungreedy (U) flag:

bc

fg

bcd(ef

And without it:

bc)de(fg

bcd(ef)g

Both variants don't do what I need. Maybe someone know how solve my issue?

  • 写回答

3条回答 默认 最新

  • dongxixiu9134 2017-04-08 13:41
    关注

    This question pretty much has the answer, but the implementations are a little ambiguous. You can use the logic in the accepted answer without the ~s to get this regex:

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

    Tested with this output:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?