0

我正在尝试获取一个正则表达式来在一行上查找我的模式的多个条目。注意:我已经使用 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 Regex 测试工具。这是我最近(徒劳的)尝试使用该程序来获得它,输出应该非常直观。

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>

我通过添加一个?after将捕获转换为非贪婪.*

但是当有疑问时,你可以使用这个技巧:

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

[^"]*表示任意数量的非双引号
[^<]*字符 表示任意数量的非左角字符

所以你避免担心贪婪/非贪婪

于 2011-08-05T04:24:37.547 回答