正则表达式判断字符串末尾是否以句号结尾

2025-02-25 09:08:26
推荐回答(2个)
回答1:

提供下面的代码供参考:
import java.util.regex.*;

public class Regex {
public static void main(String[] args){
String sa = new String("abc123");
String sb = new String("abc123a");

Pattern pattern = Pattern.compile("\\d+$");
Matcher matcher = pattern.matcher(sa);

if(matcher.find()){
System.out.println("字符串sa是以数字结尾的,结尾的数字是:"+matcher.group());
}
else{
System.out.println("字符串sa不是以数字结尾的");
}

matcher.reset(sb);
if(matcher.find()){
System.out.println("字符串sb是以数字结尾的,结尾的数字是:"+matcher.group());
}
else{
System.out.println("字符串sb不是以数字结尾的");
}
}
}
运行结果如下:
字符串sa是以数字结尾的,结尾的数字是:123
字符串sb不是以数字结尾的

回答2:

String str="";
String reg=".*[\\.。]$";
if(str.matches(reg)){
//todo

}