2012-01-12 84 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