HTMLタグ正規表現パターン

<("[^"]** "|'[^']** '|[^'">])** >

説明

<        #start with opening tag "<"
 (      #   start of group #1
   "[^"]** "  #   allow string with double quotes enclosed - "string"
   |        #   ..or
   '[^']** '  #   allow string with single quote enclosed - 'string'
   |        #   ..or
  [^'">]   #   cant contains one single quotes, double quotes and ">"
 )      #   end of group #1
 **       # 0 or more
>        #end with closing tag ">"

HTMLタグは、 “single”または “二重引用符”を囲まずに開始タグで始まります。最後に、終了タグ “>”で終了します。

Javaの正規表現の例

package com.mkyong.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLTagValidator{

   private Pattern pattern;
   private Matcher matcher;

   private static final String HTML__TAG__PATTERN = "<(\"[^\"]** \"|'[^']** '|[^'\">])** >";

   public HTMLTagValidator(){
      pattern = Pattern.compile(HTML__TAG__PATTERN);
   }

 /** **
   **  Validate html tag with regular expression
   **  @param tag html tag for validation
   **  @return true valid html tag, false invalid html tag
   ** /  public boolean validate(final String tag){

      matcher = pattern.matcher(tag);
      return matcher.matches();

  }
}

一致するHTMLタグ:

{空} 1。 “<b>”、 “<input value = ‘>’>” <input value = ‘<‘> “、” <b/> “3. <a href = ‘http://www.google > <input value = “id =” test “>” <input value = “test”> ” ”

HTMLタグが一致しません:

{空} 1。 “<input value = ‘id =’ test ‘>” – 一重引用符文字列は許可されていません+ 3. ” <input value ⇒> ” – 単一開始タグ>は許可されていません。一重引用符または二重引用符で囲む必要があります

ユニットテスト – HTMLTagValidatorTest

package com.mkyong.regex;

import org.testng.Assert;
import org.testng.annotations.** ;
 /** **
 **  HTMLTag validator Testing
 **  @author mkyong
 **
 ** /public class HTMLTagValidatorTest {

    private HTMLTagValidator htmlTagValidator;

    @BeforeClass
        public void initData(){
        htmlTagValidator = new HTMLTagValidator();
        }

    @DataProvider
    public Object[][]ValidHTMLTagProvider() {
           return new Object[][]{
           new Object[]{"<b>"},
                   new Object[]{"<input value='>'>"},
           new Object[]{"<input value='<'>"},
           new Object[]{"<b/>"},
                   new Object[]{"<a href='http://www.google.com'>"},
           new Object[]{"<br>"},
                   new Object[]{"<br/>"},
           new Object[]{"<input value=\"\" id='test'>"},
                   new Object[]{"<input value='' id='test'>"}
       };
    }

    @DataProvider
    public Object[][]InvalidHTMLTagProvider() {
        return new Object[][]{
          new Object[]{"<input value=\" id='test'>"},
          new Object[]{"<input value=' id='test'>"},
          new Object[]{"<input value=> >"}
        };
    }

    @Test(dataProvider = "ValidHTMLTagProvider")
    public void ValidHTMLTagTest(String tag) {

        boolean valid = htmlTagValidator.validate(tag);
        System.out.println("HTMLTag is valid : " + tag + " , " + valid);
        Assert.assertEquals(true, valid);

    }

    @Test(dataProvider = "InvalidHTMLTagProvider",
                 dependsOnMethods="ValidHTMLTagTest")
    public void InValidHTMLTagTest(String tag) {

       boolean valid = htmlTagValidator.validate(tag);
       System.out.println("HTMLTag is valid : " + tag + " , " + valid);
       Assert.assertEquals(false, valid);

    }
}

ユニットテスト – 結果

HTMLTag is valid : <b> , true
HTMLTag is valid : <input value='>'> , true
HTMLTag is valid : <input value='<'> , true
HTMLTag is valid : <b/> , true
HTMLTag is valid : <a href='http://www.google.com'> , true
HTMLTag is valid : <br> , true
HTMLTag is valid : <br/> , true
HTMLTag is valid : <input value="" id='test'> , true
HTMLTag is valid : <input value='' id='test'> , true
HTMLTag is valid : <input value=" id='test'> , false
HTMLTag is valid : <input value=' id='test'> , false
HTMLTag is valid : <input value=> > , false
PASSED: ValidHTMLTagTest("<b>")
PASSED: ValidHTMLTagTest("<input value='>'>")
PASSED: ValidHTMLTagTest("<input value='<'>")
PASSED: ValidHTMLTagTest("<b/>")
PASSED: ValidHTMLTagTest("<a href='http://www.google.com'>")
PASSED: ValidHTMLTagTest("<br>")
PASSED: ValidHTMLTagTest("<br/>")
PASSED: ValidHTMLTagTest("<input value="" id='test'>")
PASSED: ValidHTMLTagTest("<input value='' id='test'>")
PASSED: InValidHTMLTagTest("<input value=" id='test'>")
PASSED: InValidHTMLTagTest("<input value=' id='test'>")
PASSED: InValidHTMLTagTest("<input value=> >")

===============================================
    com.mkyong.regex.HTMLTagValidatorTest
    Tests run: 12, Failures: 0, Skips: 0
===============================================


===============================================
mkyong
Total tests run: 12, Failures: 0, Skips: 0
===============================================

正規表現の詳細を知りたいですか?このベストと古典的な本を強くお勧めします – “正規表現のマスター”

+

+