【问题标题】:Regex expression to get the number out of the string [closed]正则表达式从字符串中获取数字[关闭]
【发布时间】:2013-07-31 16:14:40
【问题描述】:

我有一个如下字符串

            XXXX456

例如我有时会得到字符串

       <ignore>456

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

【问题讨论】:

标签: java regex


【解决方案1】:

试试:

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

【讨论】:

    【解决方案2】:

    试试这个:

    "([0-9]+)$"
    

    将匹配末尾的任何数字。

    【讨论】:

    • 如果你有一个像 "123foo456bar" 这样的字符串怎么办?
    • 抱歉,点是错字。
    【解决方案3】:

    试试这个:

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

    【讨论】:

      【解决方案4】:
      Pattern p = Pattern.compile("-?\\d+");
      Matcher m = p.matcher("<ignore>456");       
      while (m.find()) {
          System.out.println(m.group());
      }
      

      【讨论】:

        猜你喜欢
        • 2014-04-09
        • 1970-01-01
        • 2020-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-07
        • 2014-03-04
        • 1970-01-01
        相关资源
        最近更新 更多