2016-06-16 25 views
1

正規表現を使用すると、jspファイル内のhtmlタグの一致に少し問題があります。
私はjspファイルのコメント行を破棄して、一致する行を取得します。
1.キャッチコメント行ではなく、正規表現の一致グループでは、
2.キャッチラインだけ後のコメント行:
はここに私のテストhtmlタグ、正規表現と一致し、特定のhtmlタグで複数行を破棄

状態です。

`` `

[前]

<%-- 

    <img src="/x.gif" alt='x.gif'> 
    <input type="text" name="x" value="y"> 
--%> 
    <img src="/x.gif" alt='x.gif'> 
    <input type="text" name="x" value="y"> 

助けを

<img src="/x.gif" alt='x.gif'> 
    <input type="text" name="x" value="y"> 

` ``

TKS [後]。 :)

+0

他のテキストつまり、それらは違う何か他の言葉で...あなたがそれらを維持したいように特別ですあなたはマッチしたくないですか? –

+0

私は正規表現マッチマルチラインを使用したいと私は問題を使用して?:?!パターンマッチ、私はそれをfiguareする方法を知りません。 :< –

答えて

0

説明

<%--((?:<\/?[a-z]+(?=[\s>])(?:[^>=]|=(?:'[^']*'|"[^"]*"|[^'"\s]*))*\s?\/?>|[^<])*?)--%> 

Regular expression visualization

この正規表現は、次の操作を行います:

  • <%----%>タグ
  • 内括弧コメントの内容全体を節約見つけますグループ1をキャプチャするコメントタグ は難しいHTMLのパターンマッチング

ライブデモ

https://regex101.com/r/fH2xU5/3

サンプルテキストを作ることが困難エッジケースを回避

<span><a onmouseover=' if (3 > a) { var string=" <img src=NotTheDroidYouAreLookingFor.jpg>; "; } ' href="link.html">This is a droid I'm looking for: <img src=DesiredDroids.png></a> 
</span> 

<%-- 

    <img onmouseover=' if (3 > a) { var string=" --%> <img src=NotTheDroidYouAreLookingFor.jpg>; "; } ' src="/x.gif" alt='x.gif'> 
    <input type="text" name="x" value="y"> 
--%> 

No start comment tag 

    <img src="/x.gif" alt='x.gif'> 
    <input type="text" name="x" value="y"> 
--%> 

<%-- 

    <img src="/x.gif" alt='x.gif'> 
    <input type="text" name="x" value="y"> 
--%> 
必要なテキストでどのような機能

サンプルと一致し

MATCH 1 
1. [196-369] ` 

    <img onmouseover=' if (3 > a) { var string=" --%> <img src=NotTheDroidYouAreLookingFor.jpg>; "; } ' src="/x.gif" alt='x.gif'> 
    <input type="text" name="x" value="y"> 
` 

MATCH 2 
1. [481-557] ` 

    <img src="/x.gif" alt='x.gif'> 
    <input type="text" name="x" value="y"> 

説明

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

オハイオ州の私の神...ツールとそれはとてもプロ、たくさんのtksだ。 しかし、私はhtmlのコメント部分を破棄したい。 –

+0

OKだから、テキストの '<% - 'と ' - %>'部分を削除すると同時に、コメントタグの中にあるテキストを保持したいのですか? –

+0

申し訳ありませんが私の悪い表現、 私は<%-- and %-->の間に行をドロップし、試合の行の直後に一致を取得したい。 私は行クラス= "msg"を探していますが、その前または後にid = "msg"はありません。 私はregを次のように書いています: (?!。* id = "msg")^((*。*)(class = "msg"(s))>)(?!。* id = "msg" ) と私は私の必要性に合うようにIDを追加するために置き換えを書いた: $ 2id = "msg" $ 3 $ 4> –

関連する問題