使用Java正则表达式提取字符串末尾的数字一例

时间:2014-12-26 14:31:49   收藏:0   阅读:931

直接上代码:

String reg = "\\D+(\\d+)$";    //提取字符串末尾的数字:封妖塔守卫71 == >> 71  
String s = monster.getMonsterName();  
Pattern p2 = Pattern.compile(reg);  
Matcher m2 = p2.matcher(s);  
int historyHighestLevel = 1;
if(m2.find()){  
    historyHighestLevel = Integer.parseInt(m2.group(1));
    System.out.println(m2.group(1));  // 组提取字符串  
} 

相似的我也可以提起字符串中间的一个或多个数字:

reg = "\\D+(\\d+)$\\D+";  // 提起字符串中间的数字
reg = "\\D+(\\d+)$\\D+(\\d+)\\D*";  // 提起字符串中间的多个数字

原文:http://www.cnblogs.com/digdeep/p/4186647.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!