我想用java在html頁面中找到某個標籤。我所知道的是什麼樣的標籤(div,span ...)和id ...我不知道它是怎麼樣的,有多少個空格是標籤中的哪個或哪些是什麼...所以我想過使用模式匹配我有以下代碼:模式匹配java:不起作用
// <tag[any character may be there or not]id="myid"[any character may be there or not]>
String str1 = "<" + Tag + "[.*]" + "id=\"" + search + "\"[.*]>";
// <tag[any character may be there or not]id="myid"[any character may be there or not]/>
String str2 = "<" + Tag + "[.*]" + "id=\"" + search + "\"[.*]/>";
Pattern p1 = Pattern.compile(str1);
Pattern p2 = Pattern.compile(str2);
Matcher m1 = p1.matcher(content);
Matcher m2 = p2.matcher(content);
int start = -1;
int stop = -1;
String Anfangsmarkierung = null;
int whichMatch = -1;
while(m1.find() == true || m2.find() == true){
if(m1.find()){
//System.out.println(" ... " + m1.group());
start = m1.start();
//ende = m1.end();
stop = content.indexOf("<", start);
whichMatch = 1;
}
else{
//System.out.println(" ... " + m2.group());
start = m2.start();
stop = m2.end();
whichMatch = 2;
}
}
,但我得到與M1(M2)。開始(),當我進入沒有實際的標籤,我逼債得到任何東西,當我進入一個例外[*]正則表達式:(......我真的沒有找到這個解釋...我還沒有用模式或匹配的所有,所以我有點失落,沒有發現任何東西到目前爲止。如果有人可以解釋我會很棒我做錯了什麼或者我怎麼能做得更好...
thnx提前:)
... DG
thnx爲代碼:)真棒 – doro 2009-07-03 10:23:00