【问题标题】:Java Matcher Digit methodJava Matcher 数字方法
【发布时间】:2011-10-14 07:05:43
【问题描述】:

如何匹配字符串中的数字整数并返回整数值。? 例如;

String = "Color Red, Size 32 / Text text";

我想从这个字符串中得到“32”整数值。

非常感谢。

问候, 可可

【问题讨论】:

    标签: java regex matcher


    【解决方案1】:
    public static void main(String[] args) {
        String s = "Color Red, Size 32 / Text text";
        Matcher matcher = Pattern.compile("\\d+").matcher(s);
        if (matcher.find()) {
          String find = matcher.group();
          Integer i = Integer.parseInt(find);
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个代码

      Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
      Matcher m = p.matcher("Testing1234Testing");
      
      if (m.find()) {
          System.out.println(m.group(1));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-25
        • 2013-04-10
        相关资源
        最近更新 更多