zoukankan      html  css  js  c++  java
  • 正则表达式对字符串的常见操作_获取

    正则表达式常见的规则查看API

    将正则规则进行对象的封装。Pattern p = Pattern.compile("a*b");
    通过正则对象的matcher方法字符串相关联。获取要对字符串操作的匹配器对象Matcher .Matcher m = p.matcher("aaaaab");
    通过Matcher匹配器对象的方法对字符串进行操作。boolean b = m.matches();

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class RegexDemo {
        public static void main(String[] args) {        
            functionDemo();
        }
        public  static void functionDemo() {
            
            String str = "da jia hao,ming tian bu fang jia!";
            
            String regex = "\b[a-z]{3}\b";
            
            //1,将正则封装成对象。
            Pattern p = Pattern.compile(regex);
            //2, 通过正则对象获取匹配器对象。 
            Matcher m = p.matcher(str);
            
            //使用Matcher对象的方法对字符串进行操作。
            //既然要获取三个字母组成的单词 
            //查找。 find();
            System.out.println(str);
            while(m.find()){
                System.out.println(m.group());//获取匹配的子序列
                
                System.out.println(m.start()+":"+m.end());
            }
        }
  • 相关阅读:
    centos 部署.NET CORE
    nginx 负载均衡
    graylog centos7 部署
    springboot 2.x centos 7.0 部署
    HashMap源代码阅读理解
    服务器安装redis
    java ---- gradle
    uboot-makefile总览
    makeFile
    Spring 推断构造方法
  • 原文地址:https://www.cnblogs.com/LO-ME/p/3603378.html
Copyright © 2011-2022 走看看