正規表現でHTMLタグを検証する方法

正規表現でHTMLタグを検証する方法

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タグは、開始タグ「」で始まり、単一引用符または二重引用符は含まれません。 最後に、終了タグ「>」で終了します

Java正規表現の例

package com.example.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();

  }
}