正则表达式从字符串中获取数字

时间:2013-07-31 08:27:10

标签: java regex

我有一个像

这样的字符串
            XXXX456

例如,我有时会将字符串作为

       <ignore>456

我想从这个字符串中获取数字

4 个答案:

答案 0 :(得分:2)

试试这个:

String s = "<ignore>456";
System.out.println(s.replaceAll("\\D", ""));

答案 1 :(得分:1)

尝试:

yourStr = yourStr.replaceAll("[^\\d]", "");

答案 2 :(得分:0)

试试这个:

"([0-9]+)$"

将匹配最后的任何数字。

答案 3 :(得分:0)

Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher("<ignore>456");       
while (m.find()) {
    System.out.println(m.group());
}
相关问题