量词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 ""
(即缺席/零次出现a
)starting and ending at index 0
,然后匹配a
starting at index 0
and 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 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.");
}
}
}
}