方法2.正则表达式的处理:
publicvoidtestCheck();
{
//img..(其它属性);src(可以有任意空格);=
//(可以有任意空格或者一个双引号或者一个单引号);url
//(可以有任意空格或者一个双引号或者一个单引号);
//..(其它属性);
//任务一,查找img标签
//1img(0..n个空格);/
Stringregex1=img\\s*/;
Patternpattern=Pattern.compile(regex1,Pattern.DOTALL);;
Matchermatcher=pattern.matcher(img/);;
assertTrue(matcher.find(););;
matcher=pattern.matcher(img/);;
assertTrue(matcher.find(););;
//2img(1..n个空格);attr1=abc(0..n个属性);/
Stringregex2=img\\s+.*/;//(暂时对属性的匹配用.*代替);
Patternpattern2=Pattern.compile(regex2,Pattern.DOTALL);;
Matchermatcher2=pattern2.matcher(imgabc/);;
assertTrue(matcher2.find(););;
matcher2=pattern2.matcher(imgabcd/);;
assertTrue(matcher2.find(););;
//3结合1,2
Stringregex3=img(;
checkOne(regex3);;
//4img0或多个字符/img
Stringregex4=img(.*.*/img);;
checkTwo(regex4);;
//5结合3,4
Stringregex5=img((););;
checkOne(regex5);;
checkTwo(regex5);;
//-完成任务一-
//6查找属性attr=(0..n个空格);abc
Stringregex6=.+=\\s*(.*|.*|\.*\);;
Patternpattern6=Pattern.compile(regex6,Pattern.DOTALL);;
Matchermatcher6=pattern6.matcher(attr=abc);;
assertTrue(matcher6.find(););;
matcher6=pattern6.matcher(attr=abc);;
assertTrue(matcher6.find(););;
matcher6=pattern6.matcher(attr=\abc\");;
assertTrue(matcher6.find(););;
////先执行5再使用6对内容进行检索
//Stringabc=abcimgsrc=\1\/defimgurl=2/dfgimgsrc=3/imgsrc=/imgsrc=\def\df/imgdfgimg/img;
//Patternpattern8=Pattern.compile(regex5,Pattern.DOTALL);;
//Matchermatcher8=pattern8.matcher(abc);;
//while(matcher8.find(););
//{
//Strings=abc.substring(matcher8.start();,matcher8.end(););;
//System.out.println(find=+s);;
//matcher6=pattern6.matcher(s);;
//if(matcher6.find(););
//{
//for(intj=0;j=matcher6.groupCount();;j++);{
//System.out.println(group+j+=+matcher6.group(j););;
//}
//}
//}
//7结合5,6
Stringregex7=img((););;
System.out.println(regex7=+regex7);;
Patternpattern7=Pattern.compile(regex7,Pattern.DOTALL);;
Matchermatcher7=pattern7.matcher(imgabc=/img);;
assertTrue(matcher7.find(););;
matcher7=pattern7.matcher(imgabc=def/img);;
assertTrue(matcher7.find(););;
////atgroup7finddef
getAttrValue(matcher7);;
matcher7=pattern7.matcher(imgabc=\def\dsf/img);;
assertTrue(matcher7.find(););;
//atgroup7finddef
getAttrValue(matcher7);;
matcher7=pattern7.matcher(imgabc=def/img);;
assertTrue(matcher7.find(););;
//atgroup7finddef
getAttrValue(matcher7);;
matcher7=pattern7.matcher(imgabc=def/);;
assertTrue(matcher7.find(););;
//atgroup4finddef
getAttrValue(matcher7);;
matcher7=pattern7.matcher(imgabc/);;
assertFalse(matcher7.find(););;
matcher7=pattern7.matcher(imgabcd/img);;
assertFalse(matcher7.find(););;
//进一步测试多个img标签的情况,在*后加?以非贪婪模式处理,属性名、值,标签的名称按照标准允许的字符进行处理。
//8把.+=\\s*(.*|.*|\.*\);中的.+替换为src
//9根据7的结果,可能在第7组或第四组找到结果,重新拆分为两个正则表达式分别处理。
}
privatevoidgetAttrValue(Matchermatcher7);{
inti=matcher7.groupCount();;
System.out.println(groupCount=+i);;
for(intj=0;j=i;j++);
{
try{
Strings=matcher7.group(j);;
System.out.println(group+j+=+s);;
}catch(Exceptione);{
System.out.println(notfindgroup=+j);;
}
}
}
privatevoidcheckTwo(Stringregex);{
Patternpattern4=Pattern.compile(regex,Pattern.DOTALL);;
Matchermatcher4=pattern4.matcher(img/img);;
assertTrue(matcher4.find(););;
}
privatevoidcheckOne(Stringregex);{
Patternpattern3=Pattern.compile(regex,Pattern.DOTALL);;
Matchermatcher3=pattern3.matcher(img/);;
assertTrue(matcher3.find(););;
matcher3=pattern3.matcher(img/);;
assertTrue(matcher3.find(););;
matcher3=pattern3.matcher(imgabc/);;
assertTrue(matcher3.find(););;
matcher3=pattern3.matcher(imgabcd/);;
assertTrue(matcher3.find(););;
matcher3=pattern3.matcher(imgabcd/);;
assertFalse(matcher3.find(););;
}
(?!=\\s*img\\s+.*\\s+)(?=src)\\s*=\\s*(\[^\"]*(?!=\)|[^']*(?!=))
能解释到=.*的一串..
例如:
imgadsfsa=fasdfdsasrc=xiaoyuhaha==afadsfsaimgadsfsa=fasdfdsasrc=xiaoyuhaha==afadsfsa
可以得到两串=xiaoyu(因为我这里有两个src=xiaoyu)
Stringstr=imgadsfsa=fasdfdsasrc=xiaoyuhaha==afadsfsa\nimgadsfsa=fasdfdsasrc=xiaoyuhaha==afadsfsa;
Patternp=Pattern.compile((?!=\\s*img\\s+.*\\s+);(?=src);\\s*=\\s*(\[^\"]*(?!=\);|[^']*(?!=););,Pattern.CASE_INSENSITIVE|Pattern.DOTALL);;
Matcherm=p.matcher(str);;
while(m.find(););{
System.out.println(m.group(););;
}
Tags:
0 评论:
发表评论