正则表达式,求判断字符串是否以数字组结尾,并取出结尾的数字

正则表达式,求判断字符串是否以数字组结尾,并取出结尾的数字的正则表达式,最好是Java写的

第1个回答  2010-03-29
(\d+)$

即可。
括号所匹配的内容即是结尾的数字。
第2个回答  推荐于2017-11-23
提供下面的代码供参考:
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不是以数字结尾的本回答被提问者采纳
第3个回答  2010-03-29
/\d$/
第4个回答  2010-03-29
\\d\\s*$
相似回答