我想用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