我需要检查给定的字符串是否以正则表达式字符串结尾。我写了下面的代码:好的方法来检查一个字符串是否以正则表达式字符串结尾
public static void main(String[] args) {
String str = "wf-008-dam-mv1";
String regex = "mv(\\d)?$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.find() && matcher.end() == str.length()) {
System.out.println("Ends with true");
} else {
System.out.println("Ends with false");
}
}
这里的str
可以结尾有或没有数字。这是做这件事的好方法吗?