gpt4 book ai didi

java - 正则表达式同时显示不同的返回不同的结果

转载 作者:行者123 更新时间:2023-12-01 19:55:29 25 4
gpt4 key购买 nike

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Validate {

public static void main(String[] args) {

Matcher matcher;
Pattern regex;
String str="David";
regex =Pattern.compile("^[A-Za-z]*$", Pattern.CASE_INSENSITIVE);
matcher= regex.matcher(str);
System.out.println("First result="+matcher.find()); //true
System.out.println("Second result="+matcher.find()); //false

}
}

最佳答案

摘自Matcher.find()的API文档

Attempts to find the next subsequence of the input sequence that matches the pattern.

This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

If the match succeeds then more information can be obtained via the start, end, and group methods.

因此,要运行代码,您必须先调用 Matcher.reset(),然后再调用 Matcher.find()

关于java - 正则表达式同时显示不同的返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59044761/

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