量词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的一次或零次匹配。 因此,它不应与zero-length ""
匹配(即a
} starting and ending at index 0
的缺席/零次匹配,然后匹配a
starting at index 0
和{ {1}},然后是ending at index 0
""
?我认为应该这样做。
这样看起来似乎starting and ending at index 1
一直在寻找matcher
的字符串,然后当确定不再有a
时({1}}是字符串的a
?)然后它会查找end
/缺少zero occurrence
?我认为那将是乏味的,事实并非如此。但是,它应该在a
与starting and ending at 0
开头并以index 0
结尾之前找到“index 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.");
}
}
}
}
答案 0 :(得分:2)
?
量词是贪心,这意味着它会尝试找到最大可能匹配。由于匹配的部分无法重复使用,因此无法在""
之前匹配空字符串a
(您可以将其视为第一次贪婪匹配的标准),但您可以匹配空字符串在它之后。
您可以通过在此量词之后添加?
来使其不情愿,这将使其尝试找到最小可能的匹配。因此,如果您尝试查找正则表达式a??
的匹配项,您会看到0
作为第一个匹配项的索引(在a
之前将为空字符串)。