Pattern.Match

一.代码如下:

    package train;
import java.util.regex.*;
    public class Test {
        public static void main(String[] args) {
            Pattern p;//定义一个模式对象
            Matcher m;//定义一个匹配对象
            String regex = "[1-9][0-9]*[.]?[0-9]*";
            p = Pattern.compile(regex);//初始化模式对象
            String s = "Price:123.456$,weight:234.78,height:83";
            m = p.matcher(s);//初始化匹配对象
            while (m.find()) {
                String str = m.group();
                System.out.println("从" + m.start() + "到" + m.end() + "匹配模式子序列:");
                System.out.println(str);
            }
        }
    }

二.运行结果如下:
在这里插入图片描述

发布了75 篇原创文章 · 获赞 55 · 访问量 9719

猜你喜欢

转载自blog.csdn.net/weixin_43597743/article/details/103226520