例如字符串:
20元/周
100元/年

想要获取字符串中的价格,因为价格不稳定无法通过subString来截取,只能匹配其中的数字。
代码:

String code = "100元/年";
        //匹配非数字字符,然后全部替换为空字符,剩下的自然只有数字啦
        String s = Pattern.compile("[^0-9]").matcher(code).replaceAll("");
        //打印结果 100
        System.out.println(s);