2009-07-03 74 views
0

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

回答

1

这里是你想要什么,从我的笔记一个适合做一个例子:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Main { 

    public static void main(String[] args) { 

     String tag = "thetag"; 
     String id = "foo"; 

     String content = "<tag1>\n"+ 
       "<thetag name=\"Tag Name\" id=\"foo\">Some text</thetag>\n" + 
       "<thetag name=\"AnotherTag\" id=\"foo\">Some more text</thetag>\n" + 
       "</tag1>"; 

     String patternString = "<" + tag + ".*?name=\"(.*?)\".*?id=\"" + id + "\".*?>"; 

     System.out.println("Content:\n" + content); 
     System.out.println("Pattern: " + patternString); 

     Pattern pattern = Pattern.compile(patternString); 

     Matcher matcher = pattern.matcher(content); 

     boolean found = false; 
     while (matcher.find()) { 
      System.out.format("I found the text \"%s\" starting at " + 
        "index %d and ending at index %d.%n", 
        matcher.group(), matcher.start(), matcher.end()); 
      System.out.println("Name: " + matcher.group(1)); 
      found = true; 
     } 
     if (!found) { 
      System.out.println("No match found."); 
     } 
    } 
} 

你会发现,模式字符串变成像<thetag.*?name="(.*?)".*?id="foo".*?>这将名为thetag标签搜索其中id属性设置为“foo”。

注意以下几点:

  • 它使用.*?到弱匹配零个或多个的任何东西(如果你不明白,请尝试删除?到明白我的意思)。
  • 它使用括号(name="(.*?)"部分)之间的子匹配表达式来提取名称属性(作为示例)的内容。
+0

thnx为代码:)真棒 – doro 2009-07-03 10:23:00

1

我觉得每次调用find通过你的对手前进。在您的条件下调用m1.find()将您的匹配器移动到不再有效匹配的位置,这会导致m1.start()抛出(我猜测)IllegalStateException确保您每次迭代调用一次查找并引用某个标志的结果可以避免这种情况问题。

boolean m1Matched = m1.find() 
boolean m2Matched = m2.find() 
while(m1Matched || m2Matched) { 

      if(m1Matched){ 
       ... 
      } 

m1Matched = m1.find(); 
m2Matched = m2.find(); 
} 
+0

thnx,我会看看:) – doro 2009-07-03 10:02:32

3

我知道我拓宽了你的问题,但我认为,使用专用库解析HTML文件(如:http://htmlparser.sourceforge.net/)会比正则表达式更容易和准确。

+0

我敢打赌,有一些非常酷的解决方案,将带走一些离开,但我应该从头开始做... thnx,我会看看无论如何到它;) – doro 2009-07-03 10:03:24