2015-01-02 191 views
1

我想弄清楚如何匹配我需要的東西的其他部分,但似乎無法讓它工作。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 
) 
+0

所以要捕捉括號之間的所有數字?修改您的示例以顯示EXPECTED輸出 – Enissay

+0

不是。我想捕捉開始(這是一年前應該是第一個括號(1234))的所有內容,然後我想在大括號內捕捉所有內容,如果字符串中有這樣的括號。如果存在大括號,我需要+特別匹配大括號括號內的內容) – CLECode

+0

在所有示例中,「1234」位於括號內,而不是花括號。 – Barmar

回答

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 
+0

作品偉大的傢伙,謝謝。 – CLECode