PHP - 正则表达式匹配其他正则表达式中的大括号

PHP - Regex match curly brackets within other regex expression

我正在尝试找出如何匹配我需要但似乎无法正常工作的东西的其他部分。

这是我目前拥有的:

preg_match_all("/^(.*?)(?:.\(([\d]+?)[\/I^\(]*?\))(?:.\((.*?)\))?/m",$data,$r, PREG_SET_ORDER);

示例文本:

INPUT - Each line represents a line inside a text file. 
-------------------------------------------------------------------------------------
"!?Text" (1234)                                         1234-4321
"#1 Text" (1234)                                        1234-????
#2 Text (1234) {Some text (#1.1)}                       1234
Text (1234)                                             1234
Some Other Text: More Text here 1234-4321 (1234) (V)    1234

我想做的事情:

我还想匹配大括号中的内容和大括号中括号中的内容。 考虑到大括号 + 括号中的内容可能并不总是在行内,我似乎无法让它工作。

基本上第一个 (1234) 将是一年,我只想匹配它一次,但是在最后一个字符串示例中它也匹配 (V) 但我不希望它匹配。

理想的输出:

Array
(
    [0] => "!?Text" (1234)
    [1] => "!?Text"
    [2] => 1234
)
Array
(
    [0] => "#1 Text" (1234)
    [1] => "#1 Text"
    [2] => 1234
)
Array
(
    [0] => "#2 Text" (1234)
    [1] => "#2 Text"
    [2] => 1234
    [3] => Some text (#1.1) // Matches things within curly brackets if there are any.
    [4] => Some text // Extracts text before brackets
    [5] => #1.1 // Extracts text within brackets (if any because brackets may not be within curly brackets.)
)
Array
(
    [0] => Text (1234)
    [1] => Text
    [2] => 1234
)
Array // (My current regular expression gives me a 4th match with value 'V', which it shouldn't do)
(
    [0] => Some Other Text: More Text here 1234-4321 (1234) (V)
    [1] => Some Other Text: More Text here 1234-4321
    [2] => 1234
)

使用情况如何:

^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?

DEMO

  NODE                       EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to :
--------------------------------------------------------------------------------
    (                        group and capture to :
--------------------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
    )                        end of 
--------------------------------------------------------------------------------
     *                       ' ' (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \(                       '('
--------------------------------------------------------------------------------
    (                        group and capture to :
--------------------------------------------------------------------------------
      \d                       digits (0-9)
--------------------------------------------------------------------------------
                               ' '
--------------------------------------------------------------------------------
    )                        end of 
--------------------------------------------------------------------------------
    \)                       ')'
--------------------------------------------------------------------------------
  )                        end of 
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
     *                       ' ' (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \{                       '{'
--------------------------------------------------------------------------------
    (                        group and capture to :
--------------------------------------------------------------------------------
      (                        group and capture to :
--------------------------------------------------------------------------------
        .*?                      any character except \n (0 or more
                                 times (matching the least amount
                                 possible))
--------------------------------------------------------------------------------
      )                        end of 
--------------------------------------------------------------------------------
       *                       ' ' (0 or more times (matching the
                               most amount possible))
--------------------------------------------------------------------------------
      \(                       '('
--------------------------------------------------------------------------------
      (                        group and capture to :
--------------------------------------------------------------------------------
        .                        any character except \n
--------------------------------------------------------------------------------
         ?                       ' ' (optional (matching the most
                                 amount possible))
--------------------------------------------------------------------------------
      )                        end of 
--------------------------------------------------------------------------------
      \)                       ')'
--------------------------------------------------------------------------------
    )                        end of 
--------------------------------------------------------------------------------
     *                       ' ' (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \}                       '}'
--------------------------------------------------------------------------------
  )?                       end of grouping