当前位置:主页 > 软件编程 > JAVA代码 >

java正则表达式提取数字的方法实例

时间:2021-08-13 07:37:09 | 栏目:JAVA代码 | 点击:

复制代码 代码如下:

@Test
    public void test33() {
        String phoneString = "哈哈,13888889999";
        // 提取数字
        // 1
        Pattern pattern = Pattern.compile("[^0-9]");
        Matcher matcher = pattern.matcher(phoneString);
        String all = matcher.replaceAll("");
        System.out.println("phone:" + all);
        // 2
        Pattern.compile("[^0-9]").matcher(phoneString).replaceAll("");
    }

 提取张三,去除数字

复制代码 代码如下:

@Test
    public void test() {
        // 提取张三 去除数字
        String r_name3 = "张三 13599998888 000000";
        Pattern pattern = Pattern.compile("[\\d]");
        Matcher matcher = pattern.matcher(r_name3);
        System.out.println(matcher.replaceAll("").trim());
    }

您可能感兴趣的文章:

相关文章