Java 匹配器在查找匹配字符时如何遍历字符串?
量词Java 匹配器在查找匹配字符时如何遍历字符串?,java,regex,string,quantifiers,Java,Regex,String,Quantifiers,量词a?应该匹配a的一次出现或没有出现。给定的程序使用java.util.regex包将正则表达式与字符串匹配 我的问题是关于模式匹配程序/结果的输出: 程序的输出:- import java.io.InputStreamReader; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /* * Enter your regex: foo * Enter i
a?
应该匹配a的一次出现或没有出现。给定的程序使用java.util.regex
包将正则表达式与字符串匹配
我的问题是关于模式匹配程序/结果的输出:
程序的输出:-
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.");
}
}
}
}
输入您的正则表达式:a?
输入要搜索的输入字符串:a
我发现文本“a”从索引0开始,在索引1结束。
我发现文本“”从索引1开始,在索引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.");
}
}
}
}
它应该与a的一次或零次出现相匹配那么它不应该匹配一个零长度“
(即a
的缺席/零次出现)从索引0开始和结束,然后匹配a
从索引0开始和从索引0结束,然后是”
从索引1开始和结束
?
我认为应该这样做
这样看来,matcher
似乎一直在字符串中查找a
,然后当它确定不再有a
(即字符串的结尾?)时,它会查找零出现
/没有a
?我认为这将是乏味的,而事实并非如此。但是,它应该先找到一个以0开头和结尾的“,然后再匹配一个以索引0开头和以索引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.");
}
}
}
}
?
量词是贪婪的,这意味着它将试图找到最大可能的匹配项。因为匹配的部分不能重用,所以不能匹配空字符串<代码>“<代码> >代码> > <代码>(您可以认为它是第一贪婪匹配的标准),但是您可以在它之后匹配空字符串。p>
你可以在这个量词后面加上?
,使它不情愿,这将使它尝试找到最小可能的匹配。因此,如果您尝试查找正则表达式a???
的匹配项,您将看到0
作为第一个匹配项的索引(在a
之前为空字符串)。非常感谢。我在学习教程,刚开始的时候,不知道贪婪的量词是什么,但这个答案澄清了它。嗨,我又困惑了。您说过a
之前的“
字符串已经是第一次贪婪匹配的一部分。我想不是,第一个贪婪的匹配只找到了a
,而不是它前面的“
。之前的如何成为第一次贪婪匹配的一部分?@Zarah我的意思不是因为?
正在寻找最大可能的匹配,因为可以在这里使用,无法返回。让我们以这种方式可视化它:我们有regexa{1,3}
和string“aaa”
。正则表达式引擎将找到一个
并在临时匹配中接受它,但还不会返回它(引擎不知道这是否是最大匹配),因此它将测试下一个字符。由于它找到了另一个a
,它将其包含在当前匹配中(因此它执行类似“a”+“a”
)的操作,因此它现在有aa
,而且它还没有返回匹配,因为它想测试下一个字符。然后再次找到a
so@Zarah它将添加到当前匹配中。现在这是可以找到的最大匹配,所以引擎返回它(“aaa”)。类似的情况也发生在a?
上,它是a{0,1}
,在启动时,正则表达式引擎会找到与casea{0}
匹配的空字符串,但还没有返回它,因为它想检查是否存在更大匹配的可能性(它是贪婪的,所以它想找到最大匹配)。因为它找到了一个a
,所以将其添加到匹配项中。换句话说,它将此a
添加到已匹配的空字符串“
”,这就是为什么我说空字符串是匹配的一部分。@Pschemo真的谢谢你!