2016-05-17 5 views
-1

[value]と一致する2つの正規表現と、html属性に一致する別の正規表現があるが、それらを1つの正規表現に結合する必要がある。PHP preg_replaceはhtmlで一致を見つけるが、html属性でない場合

これは私が[value]

$tagregexp = '[a-zA-Z_\-][0-9a-zA-Z_\-\+]{2,}'; 

    $pattern = 
      '\\['        // Opening bracket 
     . '(\\[?)'       // 1: Optional second opening bracket for escaping shortcodes: [[tag]] 
     . "($tagregexp)"      // 2: Shortcode name 
     . '(?![\\w-])'      // Not followed by word character or hyphen 
     . '('        // 3: Unroll the loop: Inside the opening shortcode tag 
     .  '[^\\]\\/]*'     // Not a closing bracket or forward slash 
     .  '(?:' 
     .   '\\/(?!\\])'    // A forward slash not followed by a closing bracket 
     .   '[^\\]\\/]*'    // Not a closing bracket or forward slash 
     .  ')*?' 
     . ')' 
     . '(?:' 
     .  '(\\/)'      // 4: Self closing tag ... 
     .  '\\]'       // ... and closing bracket 
     . '|' 
     .  '\\]'       // Closing bracket 
     .  '(?:' 
     .   '('      // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags 
     .    '[^\\[]*+'    // Not an opening bracket 
     .    '(?:' 
     .     '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag 
     .     '[^\\[]*+'   // Not an opening bracket 
     .    ')*+' 
     .   ')' 
     .   '\\[\\/\\2\\]'    // Closing shortcode tag 
     .  ')?' 
     . ')' 
     . '(\\]?)';       // 6: Optional second closing bracket for escaping shortcodes: [[tag]] 

example here

属性と値が一致する(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?この正規表現を検索して働いている正規表現です。 example here

私は、次の例

  • <div [value] ></div>
  • <div>[value]</div>

しかし

  • この例では、一致を見つけることができませんで[value]にマッチする正規表現をしたいと思います

はちょうどあなたが定期的にhtmlコードを解析しようとしているように見える表面には、私のpreg_replace_callback

preg_replace_callback($pattern, replace_matches, $html); 
+0

は、あなたがこのためにパーサを使用することを検討していますか? – chris85

+0

これはPHP文字列であり、Java文字列ではありません。すべてを二重にエスケープする必要はありません。連結を使用する代わりに、x修飾子を使用してください(もしあなたがnowdoc文字列を使用できる場合)。 html(またはxml)を扱いたい場合は、正規表現を忘れてDOMDocument(そして最終的にDOMXPath)を使います。 –

+0

他のもの、閉じる角括弧は特殊文字ではありません、あなたはそれをエスケープする必要はありません。文字クラスの中の角括弧は何も特別なものはありません。 '[^ \] []の代わりに' [^ [] 'と書くことができます。 *([^]] 'と' []] 'を書くことさえできます。なぜなら、最初の位置では閉じた角括弧がリテラル文字として見えるからです。)* –

答えて

1

Foreward

で使用するために、単一の正規表現にそれをする必要があります表現。私は正規表現を使ってHTMLを解析することはお勧めできませんが、曖昧な可能性があるため、HTMLを何らかの形でコントロールしていると思われます。正規表現の警察が泣いている最端のケース。

  • [some value]
    • 角括弧内の部分文字列をキャプチャするには、[value]がAにありました。この正規表現は、次のように

      説明

      <\w+\s(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\[(?<DesiredValue>[^\]]*)\]) 
      | 
      <\w+\s?(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*> 
      (?:(?!<\/div>)(?!\[).)*\[(?<DesiredValue>[^\]]*)\] 
      

      Regular expression visualization

      を行います[value]タグストリングを提供

    • の属性領域内に別の値内にネストされていないではないですた
    • タグのttributesは<input attrib=" [value] ">
  • 捕捉サブラッピング角括弧を含まない
  • 任意のタグ名を許可、または
  • valueは、任意の文字列であることを許可したいタグ名で\wを置き換える
  • 避ける難しいエッジケース

注:この正規表現は、最高の以下のフラグと一緒に使用されています

  • は、式の中の空白を無視

    • グローバル
    • ドットが一致する新しい行
    • 重複キャプチャグループを許可する

    ライブデモ

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

    サンプルテキスト

    <div [value 1] ></div> 
    <div>[value 2]</div> 
    but not find a match in this example 
    
    <div attr="attribute[value 3]"/> 
    <img [value 4]> 
    <a href="http://[value 5]">[value 6]</a> 
    

    サンプル

    MATCH 1 
    DesiredValue [6-13] `value 1` 
    MATCH 2 
    DesiredValue [29-36] `value 2` 
    MATCH 3 
    DesiredValue [121-128] `value 4` 
    MATCH 4 
    DesiredValue [159-166] `value 6` 
    
    にマッチします

    説明

    NODE      EXPLANATION 
    ---------------------------------------------------------------------- 
        <div      '<div' 
    ---------------------------------------------------------------------- 
        \s      whitespace (\n, \r, \t, \f, and " ") 
    ---------------------------------------------------------------------- 
        (?=      look ahead to see if there is: 
    ---------------------------------------------------------------------- 
        (?:      group, but do not capture (0 or more 
              times (matching the least amount 
              possible)): 
    ---------------------------------------------------------------------- 
         [^>=]     any character except: '>', '=' 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
         ='      '=\'' 
    ---------------------------------------------------------------------- 
         [^']*     any character except: ''' (0 or more 
               times (matching the most amount 
               possible)) 
    ---------------------------------------------------------------------- 
         '      '\'' 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
         ="      '="' 
    ---------------------------------------------------------------------- 
         [^"]*     any character except: '"' (0 or more 
               times (matching the most amount 
               possible)) 
    ---------------------------------------------------------------------- 
         "      '"' 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
         =      '=' 
    ---------------------------------------------------------------------- 
         [^'"]     any character except: ''', '"' 
    ---------------------------------------------------------------------- 
         [^\s>]*     any character except: whitespace (\n, 
               \r, \t, \f, and " "), '>' (0 or more 
               times (matching the most amount 
               possible)) 
    ---------------------------------------------------------------------- 
        )*?      end of grouping 
    ---------------------------------------------------------------------- 
        \[      '[' 
    ---------------------------------------------------------------------- 
        (      group and capture to \1: 
    ---------------------------------------------------------------------- 
         [^\]]*     any character except: '\]' (0 or more 
               times (matching the most amount 
               possible)) 
    ---------------------------------------------------------------------- 
        )      end of \1 
    ---------------------------------------------------------------------- 
        \]      ']' 
    ---------------------------------------------------------------------- 
    )      end of look-ahead 
    ---------------------------------------------------------------------- 
    |      OR 
    ---------------------------------------------------------------------- 
        <div      '<div' 
    ---------------------------------------------------------------------- 
        \s?      whitespace (\n, \r, \t, \f, and " ") 
              (optional (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        (?:      group, but do not capture (0 or more times 
              (matching the most amount possible)): 
    ---------------------------------------------------------------------- 
        [^>=]     any character except: '>', '=' 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
        ='      '=\'' 
    ---------------------------------------------------------------------- 
        [^']*     any character except: ''' (0 or more 
              times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        '      '\'' 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
        ="      '="' 
    ---------------------------------------------------------------------- 
        [^"]*     any character except: '"' (0 or more 
              times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        "      '"' 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
        =      '=' 
    ---------------------------------------------------------------------- 
        [^'"]     any character except: ''', '"' 
    ---------------------------------------------------------------------- 
        [^\s>]*     any character except: whitespace (\n, 
              \r, \t, \f, and " "), '>' (0 or more 
              times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
    )*      end of grouping 
    ---------------------------------------------------------------------- 
        >      '>' 
    ---------------------------------------------------------------------- 
        (?:      group, but do not capture (0 or more times 
              (matching the most amount possible)): 
    ---------------------------------------------------------------------- 
        (?!      look ahead to see if there is not: 
    ---------------------------------------------------------------------- 
         <      '<' 
    ---------------------------------------------------------------------- 
         \/      '/' 
    ---------------------------------------------------------------------- 
         div>      'div>' 
    ---------------------------------------------------------------------- 
        )      end of look-ahead 
    ---------------------------------------------------------------------- 
        (?!      look ahead to see if there is not: 
    ---------------------------------------------------------------------- 
         \[      '[' 
    ---------------------------------------------------------------------- 
        )      end of look-ahead 
    ---------------------------------------------------------------------- 
        .      any character 
    ---------------------------------------------------------------------- 
    )*      end of grouping 
    ---------------------------------------------------------------------- 
        \[      '[' 
    ---------------------------------------------------------------------- 
        (      group and capture to \2: 
    ---------------------------------------------------------------------- 
        [^\]]*     any character except: '\]' (0 or more 
              times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
    )      end of \2 
    ---------------------------------------------------------------------- 
        \]      ']' 
    
  • +0

    信じられないほどの答え、私はあなたの答えに入れられた時間と努力の量を感謝します。私はまだそれをかなり解決していないが、これはかなり助けるべきである。 – TarranJones

    +0

    この回答が欠落しているか、どうすればよいか教えてください。 –

    関連する問題