欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-21 13:40:40
复制代码 代码如下:@test    public void test33() {     ...

复制代码 代码如下:

@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());
    }

上一篇:

下一篇: