0

行上のパターンの複数のエントリを見つけるために正規表現を取得しようとしています。注:私は約1時間正規表現を使用しています... = /

例えば:

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

2回一致する必要があります:

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

答えは、貪欲vs嫌悪vs所有格の適切な習得にあると思いますが、それを機能させることができないようです...

私は近くにいると思います。これまでに作成した正規表現の文字列は次のとおりです。

(<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です:

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);
        }

    }
}
4

1 に答える 1

1

これを試して:

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

?アフターを追加して、キャプチャを貪欲でないものに変換しました.*

しかし、疑わしい場合は、次のトリックを使用できます。

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

[^"]*二重引用符ではない
[^<]*任意の数の文字を意味します左角ではない任意の数の文字を意味します

だからあなたは貪欲/非貪欲について心配することを避けます

于 2011-08-05T04:24:37.547 に答える