PHP - регулярное выражение соответствует фигурным скобкам в другом выражении регулярного выражения

Я пытаюсь понять, как сопоставить другие части того, что мне нужно, но, похоже, не могу заставить его работать.

Это то, что у меня есть до сих пор:

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
)

person CLECode    schedule 02.01.2015    source источник
comment
Итак, вы хотите захватить все числа в скобках? Измените свои примеры, чтобы показать ОЖИДАЕМЫЙ вывод   -  person Enissay    schedule 02.01.2015
comment
Не совсем. Я хочу захватить начало (то есть все до года, которое должно быть первыми скобками (1234)), затем я хочу захватить все в фигурных скобках, если в строке есть такие скобки. Если существуют фигурные скобки, мне нужно, чтобы + конкретно соответствовали вещам в скобках фигурных скобок)   -  person CLECode    schedule 02.01.2015
comment
Во всех ваших примерах 1234 находится внутри круглых скобок, а не фигурных скобок.   -  person Barmar    schedule 02.01.2015
comment
Откуда 4321 в первом примере?   -  person Barmar    schedule 02.01.2015
comment
Я обновил желаемый вывод, надеюсь, теперь он имеет больше смысла.   -  person CLECode    schedule 02.01.2015


Ответы (1)


Как насчет использования:

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

DEMO

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