0

量词a?应该匹配a single or no occurrence of a。给定的程序使用java.util.regex包将正则表达式与字符串匹配。

我的问题是关于模式匹配的程序/结果的输出:

程序的输出:-

Enter your regex: a? Enter input string to search: a I found the text "a" starting at index 0 and ending at index 1. I found the text "" starting at index 1 and ending at index 1.

问题:-

它应该匹配一个或零个出现的 a。那么它不应该匹配 a zero-length ""(即缺席/零次出现astarting and ending at index 0,然后匹配a starting at index 0and ending at index 0,然后匹配 a"" starting and ending at index 1吗?我认为应该。

这样,似乎matcher一直在寻找a'sthough-out 字符串,然后当确定没有更多a's (那是end字符串的?)时,它会寻找zero occurrence/ 不存在a? 我认为这会很乏味,而且情况并非如此。但是它应该在匹配开始于和结束于starting and ending at 0之前找到一个“” ?index 0index 1

程序:-

import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 *  Enter your regex: foo
 *  Enter input string to search: foo
 *  I found the text foo starting at index 0 and ending at index 3.
 * */

public class RegexTestHarness {

    public static void main(String[] args){

        /*Console console = System.console();
        if (console == null) {
            System.err.println("No console.");
            System.exit(1);
        }*/

        while (true) {

            /*Pattern pattern = 
            Pattern.compile(console.readLine("%nEnter your regex: ", null));*/

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

            Scanner scanner = new Scanner(new InputStreamReader(System.in));

            Pattern pattern = Pattern.compile(scanner.next());

            System.out.print("\nEnter your input string to seacrh: ");

            Matcher matcher = 
            pattern.matcher(scanner.next());

            System.out.println();

            boolean found = false;
            while (matcher.find()) {
                /*console.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("I found the text \"" + matcher.group() + "\" starting at index " + matcher.start() + " and ending at index " + matcher.end() + "."); 

                found = true;
            }
            if(!found){
                //console.format("No match found.%n", null);
                System.out.println("No match found."); 
            }
        }
    }
}
4

1 回答 1

2

?量词是贪婪的,这意味着它将尝试找到最大可能的匹配。由于无法重复使用匹配的部分,因此您""之前无法匹配空字符串a(您可以将其视为第一个贪婪匹配的 par),但您可以在它之后匹配空字符串。

您可以通过在此量词之后添加使其不情愿?,这将使其尝试找到最小的可能匹配。因此,如果您尝试查找正则表达式的匹配项,a??您将看到0第一个匹配项的索引(之前将是空字符串a)。

于 2014-06-19T19:45:08.893 回答