龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

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

时间:2014-06-05 15:11来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了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());
    }

精彩图集

赞助商链接