2016-06-19 15 views
-2

假設有像以下各項在已經匹配的模式中可以執行正則表達式嗎?

Some content here 
[shortcode_1 attr1="val1" attr2="val2"] 

    [shortcode_2 attr3="val3" attr4="val4"] 

     Some text 

    [/shortcode_2] 

[/shortcode_1] 
Some more content here 

我的問題是假設我匹配簡碼圖案,使得我得到的輸出[shortcode_1] ... [/ shortcode_1] WordPress的簡碼的內容。但是,我可以在同一次運行中使用相同的正則表達式模式獲得[shortcode_2] ... [/ shortcode_2],還是必須使用第一次運行的輸出再次運行它?

+0

根據正則表達式引擎,但我想看看INT組和條件/可選組... – Dilettant

回答

1

描述

您可以創建幾個捕獲組。一個用於整場比賽,第二個用於下屬比賽。當然,這種方法確實存在侷限性,可能會陷入一些非常複雜的邊緣情況。

(\[shortcode_1\s[^\]]*].*?(\[shortcode_2\s.*?\[\/shortcode_2\]).*?\[\/shortcode_1\]) 

Regular expression visualization

例子

現場演示

https://regex101.com/r/bQ0vV2/1

示例文字

[shortcode_1 attr1="val1" attr2="val2"] 

    [shortcode_2 attr3="val3" attr4="val4"] 

     Some text 

    [/shortcode_2] 

[/shortcode_1] 

樣品匹配

捕獲組1得到shortcode_1 捕獲組2得到shortcode_2

1. [0-139] `[shortcode_1 attr1="val1" attr2="val2"] 

    [shortcode_2 attr3="val3" attr4="val4"] 

     Some text 

    [/shortcode_2] 

[/shortcode_1]` 
2. [45-123] `[shortcode_2 attr3="val3" attr4="val4"] 

     Some text 

    [/shortcode_2]` 

說明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    \[      '[' 
---------------------------------------------------------------------- 
    shortcode_1    'shortcode_1' 
---------------------------------------------------------------------- 
    \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
    [^\]]*     any character except: '\]' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    ]      ']' 
---------------------------------------------------------------------- 
    .*?      any character (0 or more times (matching 
          the least amount possible)) 
---------------------------------------------------------------------- 
    (      group and capture to \2: 
---------------------------------------------------------------------- 
     \[      '[' 
---------------------------------------------------------------------- 
     shortcode_2    'shortcode_2' 
---------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
     .*?      any character (0 or more times 
           (matching the least amount possible)) 
---------------------------------------------------------------------- 
     \[      '[' 
---------------------------------------------------------------------- 
     \/      '/' 
---------------------------------------------------------------------- 
     shortcode_2    'shortcode_2' 
---------------------------------------------------------------------- 
     \]      ']' 
---------------------------------------------------------------------- 
    )      end of \2 
---------------------------------------------------------------------- 
    .*?      any character (0 or more times (matching 
          the least amount possible)) 
---------------------------------------------------------------------- 
    \[      '[' 
---------------------------------------------------------------------- 
    \/      '/' 
---------------------------------------------------------------------- 
    shortcode_1    'shortcode_1' 
---------------------------------------------------------------------- 
    \]      ']' 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
+0

謝謝對於這樣的詳細描述。但是,這並沒有解決問題。這實際上是考慮到簡碼名稱是* shortcode_1 *和* shortcode_2 *。我要求任何短碼名稱,明顯可以被捕獲組接收。此外,這裏的考慮是隻有一個級別的兒童短代碼。我試圖得到任何數量的子短碼的匹配。 –

+0

你原來的問題是'假設我匹配短碼型,以便得到輸出[shortcode_1] .... [/ shortcode_1]。但是,我可以在同一個運行中使用相同的正則表達式模式獲得[shortcode_2] ... [/ shortcode_2],還是必須使用第一次運行的輸出重新運行它。這聽起來像是你要問如何在同一個捕獲中獲得外部shortcode_1和內部shortcode_2。你可以編輯問題和示例文本來覆蓋你正在尋找什麼,以及期望的匹配嗎? –