2016-06-19 5 views
-2

following-すでに一致したパターンの中で正規表現を検索できますか?

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 

のようなWordPressのショートコンテンツがあると私の質問は、私は、出力[shortcode_1] .... [/ shortcode_1]を取得するようなショートパターンに一致すると仮定しています。しかし、[shortcode_2] ... [/ shortcode_2]を同じ実行で同じ正規表現パターンを使用して取得することはできますか?最初の実行からの出力を使用して再度実行する必要がありますか?

+0

を取得しますが、私はint型になり得るマッチグループと条件付き/オプショングループ... – Dilettant

答えて

1

説明

キャプチャグループを2つ作成するだけです。 1つはマッチ全体、もう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 *であることを考慮しています。私は明らかにキャプチャグループによって受け取ることができる任意のショートコード名を求めていました。また、ここで考慮すべきことは、子供のショートコードのレベルが1つだけであることです。私はいくつかのサブショートコードのための任意の一致を得ることを試みていた。 –

+0

あなたのオリジナルの質問は、「私が出力[shortcode_1] .... [/ shortcode_1]を得るようなショートコードパターンと一致するとします。しかし、同じ実行で同じ正規表現パターンを使用して[shortcode_2] ... [/ shortcode_2]を取得することはできますか?最初の実行からの出力を使用して再度実行する必要がありますか?これは、同じキャプチャでshortcode_1の外側とshortcode_2の内側を取得する方法を尋ねているような感じです。質問とサンプルテキストを編集して、探しているものと目的のものを正確にカバーできますか? –

関連する問題