gpt4 book ai didi

java - X?量数 : Why does a non-x give a "zero-length" match?

转载 作者:行者123 更新时间:2023-11-30 03:51:48 24 4
gpt4 key购买 nike

量词x?表示x出现一次或不出现

为了方便起见,我发布了一个测试工具,用于将正则表达式与字符串进行匹配。

与字符串 ababaaaab 相比,我对正则表达式 a? 感到困惑。

该程序的输出是:

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 感到困惑。

"The regular expression a? is not specifically looking for the letter "b"; it's merely looking for the presence (or lack thereof) of the letter "a". If the quantifier allows for a match of "a" zero times, anything in the input string that's not an "a" will show up as a zero-length match."

Reference

问题:-

第一行是可以理解的,我确实知道 b 或任何非 a 的存在就是 a 的缺失,或者 a 的出现 0 次,所以应该会导致匹配。 但是 a 的缺失(即 b 的出现)是在索引 1 和 2 之间。那么为什么文本 ""的匹配在索引 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.");
}
}
}
}

最佳答案

But the absence of a (that is the occurance of b) is between the indices 1 and 2. So why is the match of the text "" between the index 1 and 1 (in other words, why are we getting a zero-length match here)

匹配的长度是与模式匹配的输入字符串的长度。

由于没有“a”,所以只匹配了一个空字符串。

同样,该模式不匹配“非 a 字符序列”,它匹配总长度为 1 的“a”序列(可能为空)。在本例中,匹配的序列为空。

But the absence of a (that is the occurance of b)

a 的缺失并不b 的出现。 a 的缺失发生在 b 出现之前,并在 b 出现时结束。

关于java - X?量数 : Why does a non-x give a "zero-length" match?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24239573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com