2011-08-05 5 views
0

私のパターンの複数のエントリを1行に見つけるために正規表現を取得しようとしています。注:私は、たとえば時間程度... =/Regexの式を正しく取得するのに助けが必要です

のための正規表現を使用してきた:

<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 

は二回一致している必要があります:

1) <a href="G2532" id="1">back</a> 
2) <a href="G2564" id="2">next</a> 

を私は答えが適切であると思います欲張り対消極的な対所有者の熟達しているが、私はそれを働かせるように見えることはできません...

私は近いと思う私はこれまで作成した正規表現の文字列は:

(<a href=").*(" id="1">).*(</a>) 

しかし、正規表現マッチングは、文字列全体を1試合を返す...

私は以下のコードで(コンパイル)Javaの正規表現テストハーネスを持っています。このプログラムを使ってこれを取得しようとした最近の(無駄な)試みがありますが、出力はかなり直感的です。ここで

Enter your regex: (<a href=").*(" id="1">).*(</a>) 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)+ 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: ((<a href=").*(" id="1">).*(</a>))? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 
I found the text "" starting at index 63 and ending at index 63. 

Enter your regex: ((<a href=").*(" id="1">).*(</a>))+? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (((<a href=").*(" id="1">).*(</a>))+?) 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

は、Javaです:私は?.*

を追加することにより、非貪欲にキャプチャを変換しかし、ときました

<a href=".*?" id="1">.*?</a> 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

public class RegexTestHarness { 

    public static void main(String[] args){ 
     try{ 
      while (true) { 

       System.out.print("\nEnter your regex: "); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
       Pattern pattern = Pattern.compile(reader.readLine()); 

       System.out.print("Enter input string to search: "); 
       Matcher matcher = pattern.matcher(reader.readLine()); 

       boolean found = false; 
       while (matcher.find()) { 
        System.out.println("I found the text \"" + matcher.group() + "\" starting at " + 
         "index " + matcher.start() + " and ending at index " + matcher.end() + "."); 
        found = true; 
       } 
       if(!found){ 
        System.out.println("No match found."); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.exit(-1); 
     } 

    } 
} 
+1

[RegExを使ってHTMLを解析しないでください](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Bohemian

答えて

1

はこれを試してみてくださいあなたはこのトリックを使うことができます:

<a href="[^"]*" id="1">[^<]*</a> 

は、二重引用符
[^<]*ではありません任意の数の文字を意味し、左の角度でない任意の数の文字を意味

ですから、貪欲/非欲張り

心配を避けます
+0

ボヘミアン、あなたは正しい道を歩み始めました。私はあなたのテクニックを使っていましたが、id = "1"を "id =" [1-9] + "に変更しなければならないことに気づきました。 – Ryan

関連する問題