2012-01-12 85 views
-4
String inputs ="td class=\"prodcatnamelinkwrap\""; 
Pattern p = Pattern.compile("<td cellspacing=\"0\" align=\"center\" valign=\"bottom\" class=\"prodcatimagewrap\">"); 
Matcher m = p.matcher(inputs); 

应该我想要做的,以匹配这种格式是什么。 任何一个帮助我 在此先感谢基于正则表达式匹配

查找方法如何工作。 我必须从该文本中匹配td height =“45px”valign =“top”

td align =“center”height =“45px”valign =“top”>。

但它说假

For reference please find my code 


Pattern replace5 = Pattern.compile(".*\r?\n",Pattern.MULTILINE); 

Matcher matcher5 = replace5.matcher("td[@height=\"45px\"][@valign=\"top\"]".replaceAll("[\\[\\@]+"," ")); 
                pattern3 = Pattern.compile(matcher5.replaceAll(" ").replaceAll("\\//", "").replaceAll("\\]","")); 

pm3 = pattern3.matcher("<td align="center" height="45px" valign="top">"); 

if(pm3.find()) 

....It always results in false.Can anyone help me out 
+0

你有没有交换与输入模式?当你的输入只是所需模式的一个子集时,说你想匹配这种格式是没有意义的。你想改变输入以匹配模式或改变模式以匹配输入。而不是只显示代码,添加一些你想要完成的信息。 – 2012-01-12 12:11:59

+1

强制性:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Jonathan 2012-01-12 12:24:15

+0

这么简单,因为它是我要检查网页内容是否此匹配该标签的TD类= \ “prodcatnamelinkwrap \” – 2012-01-26 18:03:11

回答

1

不要重新发明轮子的所有时光。使用像jsoup这样的html解析器。

Document doc = Jsoup.connect("http://en.wikipedia.org/").get(); 
Elements newsHeadlines = doc.select("#mp-itn b a"); 
+0

作为简单,因为它是我想要查询的网页内容是否此匹配该标签的TD类= \“prodcatnamelinkwrap \ “ – 2012-01-29 20:44:02