java 正则表达式获取文本中数字

阅读更多

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

实例:

public void Test0108_03()
    {
        String input="winnt 5.1 internet winnta 5.3";//如何获得5.1
        String regex="winnta";
        double version=Double.parseDouble(getDigitAfter(input, regex));
        System.out.println("version:"+version);
    }
    public static String getDigitAfter(String input, String str)
    {
        Pattern p = Pattern.compile(str+"\\s*(\\d[\\.\\d]*\\d)");
        Matcher m = p.matcher(input);
        boolean result = m.find();
        String find_result = null;
        if (result)
        {
            find_result = m.group(1);
        }
        return find_result;
}

 

获取740981

 //"content": "????740981????????????30????????????????????????"
        String content = "????740981????????????30????????????????????????";
        Pattern p = Pattern.compile("[^\\d]+([\\d]+)[^\\d]+.*");
        Matcher m = p.matcher(content);
        boolean result = m.find();
        String find_result = null;
        if (result) {
            find_result = m.group(1);
        }

 

 

 

你可能感兴趣的:(正则表达式,获取数字)