Hi_Amos
坚持每天都在进步!!
随笔 - 224,  文章 - 2,  评论 - 192,  阅读 - 94万
复制代码
    @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());
    }

 

需求:过滤除点号外的所有非数字:

        String abc = "价格:0.00元";
        Pattern compile = Pattern.compile("\\d+\\.\\d+");
        Matcher matcher = compile.matcher(abc);
        matcher.find();
        String string = matcher.group();//提取匹配到的结果
        System.out.println(string);//0.00        

 

需求:只要提取数字其它都不需要

String abc = "手机:1319999999";    
System.out.println(abc.replaceAll("\\D", ""));//1319999999

 

 需求:提取价格出来

复制代码
package com.infomorrow.parser_datasource;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

public class test_money {
    @Test
    public void test(){
        //0
        //0.1
        //24.13
        String moneyString="1";
        Double extract_cost = extract_cost_dot(moneyString);
        System.out.println("extract_cost:"+extract_cost);
    }
    /**
     * 提取金额,规则为只提取数字和点号,必须有点号
     * 格式可以为0.0或者,11
     * @param cost
     * @return
     */
    public   Double extract_cost_dot(String cost) {
        Pattern compile = Pattern.compile("(\\d+\\.\\d+)|(\\d+)");
        Matcher matcher = compile.matcher(cost);
        matcher.find();
        return Double.valueOf(matcher.group());
    }
}
复制代码

 

 

 

 

 

posted on   Hi_Amos  阅读(38316)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示