2009-07-03 54 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