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

时间:2014-12-26 14:33:36   收藏:0   阅读:1464

直接上代码:

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*";  // 提起字符串中间的多个数字
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!