regex not matching due to repeated capturing group rather than capturing a repeated group

后端 未结 3 518
灰色年华
灰色年华 2021-01-14 22:31

I have the following regexp:

/(?:[\\[\\{]*)(?:([A-G\\-][^A-G\\]\\}]*)+)(?:[\\]\\}]*)/

with the following expression:

{A\'\'         


        
3条回答
  •  Happy的楠姐
    2021-01-14 23:23

    You already figured it out. Regarding to @sln's comment, there is no way to gather each singular match in one or different capturing groups while repeating a group in PCRE which is PHP's regex flavor. In this case only the last match is captured.

    However if asserting that braces should be there at the start and end of string is not important and you only need those values there is less work to do:

    $array = array_filter(preg_split("~(?=[A-G])~", trim("{A''BsCb}", '[{}]')));
    

    Regex:

    (?=[A-G]) # Positive lookahead to find next character be one from character class
    

    This regex will match all similar positions to output correct data on split:

    array(3) {
      [1]=>
      string(3) "A''"
      [2]=>
      string(2) "Bs"
      [3]=>
      string(2) "Cb"
    }
    

    Live demo

提交回复
热议问题