【发布时间】:2023-03-06 11:50:01
【问题描述】:
import java.util.regex.Pattern;
class HowEasy {
public boolean matches(String regex) {
System.out.println(Pattern.matches(regex, "abcABC "));
return Pattern.matches(regex, "abcABC");
}
public static void main(String[] args) {
HowEasy words = new HowEasy();
words.matches("[a-zA-Z]");
}
}
输出为假。我哪里错了?另外我想检查一个单词是否只包含字母,并且可能会或可能不会以单个句点结尾。那是什么正则表达式?
即“abc”“abc”。有效,但“abc..”无效。
我可以使用indexOf() 方法来解决它,但我想知道是否可以使用单个正则表达式。
【问题讨论】: