如何使⽤正则表达式验证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 ;
import Matcher;
import Pattern;
public class HTMLTagValidator{
private Pattern pattern;
private Matcher matcher;
private static final String HTML_TAG_PATTERN = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";
public HTMLTagValidator(){
pattern = Patternpile(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>”,“ <;输⼊值='>'>”
2。“ <;输⼊值='<'>”,“ <b />”
3.“ <a href='le'>”
4.“ <br>”,“ <br/>”
5.“ <;输⼊值= \” \” id ='测试'>”,“ <;输⼊值=” id ='测试'>”
HTML标记不匹配:
1.“ <;输⼊值= \” id =“测试”>” –不允许使⽤双引号字符串
2.“ <;输⼊值='id ='测试'>” –不允许使⽤单引号字符串
3.“ <;输⼊值=>>” –不允许单引号>,必须⽤单引号或双引号引起来单元测试– HTMLTagValidatorTest
package ;
stng.Assert;
stng.annotations.*;
/**
* HTMLTag validator Testing
* @author mkyong
*
*/
public class HTMLTagValidatorTest {
private HTMLTagValidator htmlTagValidator;html如何下载
@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='le'>"},    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='le'> , 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='le'>")
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=> >")
===============================================
HTMLTagValidatorTest
Tests run: 12, Failures: 0, Skips: 0
===============================================
===============================================
mkyong
Total tests run: 12, Failures: 0, Skips: 0
===============================================
想更多地了解正则表达式? 强烈推荐这本最好的经典书籍-“掌握正则表达式”

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。