量词的x?
意思a single or no occurance of x
。
为了方便起见,我发布了一个test harness
用于将正则表达式与字符串匹配的内容。
a?
与 string 相比,我对 regex 感到困惑ababaaaab
。
程序的输出是:
Enter your regex: a?
Enter your input string to seacrh: ababaaaab
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.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "a" starting at index 4 and ending at index 5.
I found the text "a" starting at index 5 and ending at index 6.
I found the text "a" starting at index 6 and ending at index 7.
I found the text "a" starting at index 7 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.
Enter your regex:
我对b的感到困惑。
“正则表达式 a? 不是专门寻找字母“b”;它只是寻找字母“a”的存在(或不存在)。如果量词允许匹配“a”零次,那么任何在不是“a”的输入字符串中将显示为零长度匹配。”
问题:-
第一行是可以理解的,我确实理解存在 b 或任何非 a 是不存在 a 或 0 出现 a,因此应该导致匹配。但是在索引1和2之间没有a(即b的出现)。那么为什么索引1和1之间的文本“”匹配(换句话说,为什么我们得到一个零长度在这里匹配)。根据我的推理,它应该在索引 1 和 2 之间。
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());
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.");
}
}
}
}