2016-06-19 3 views
-2

내 질문은 내가 출력 [shortcode_1] ... [/ shortcode_1]을 얻을 수 있도록 단축 코드 패턴과 일치한다고 가정한다 이미 일치 패턴 안에서 정규식 검색을 할 수 있습니까?

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_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

샘플 텍스트

,363,210
[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] 같은 regex 패턴을 사용하여 동일한 실행을 할 수 있습니까? 아니면 첫 번째 실행에서 출력을 사용하여 다시 실행해야합니까? 이것은 동일한 캡처에서 shortcode_1 바깥 쪽과 shortcode_2 바깥 쪽을 가져 오는 방법을 묻는 것처럼 들립니다. 원하는 내용과 원하는 내용을 포함하도록 질문과 샘플 텍스트를 편집 할 수 있습니까? –