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.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();
}
}