• Java(33):正则表达式


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class RegexDemo {
        public static void main(String[] args) {
            
    //        1. compile(String regex) 将给定的正则表达式编译到模式中。 
    //        2. matcher(CharSequence input) 创建匹配给定输入与此模式的匹配器。 
    //        3. matches() 尝试将整个区域与模式匹配。
            
            String str = "abc";
    //        1.编译正则规则形式
            Pattern p = Pattern.compile("abc");
    //        2.将正则进行匹配
            Matcher m = p.matcher(str);
    //        3.进行判断
            boolean b = m.matches();
            System.out.println(b);
            
            String str2 = "agx";
            Pattern p2 = Pattern.compile("[abc][efgh][xyz]");
            Matcher m2 = p2.matcher(str2);
            boolean b2 = m2.matches();
            System.out.println(b2);
            
            String str3 = "agx";
            System.out.println(str3.matches("[abc][efgh][xyz]"));
            
            String str4 = "D";
            Pattern p4 = Pattern.compile("[a-zA-Z]");
            Matcher m4 = p4.matcher(str4);
            boolean b4 = m4.matches();
            System.out.println(b4);
            
            String str5 = "5";
            System.out.println(str5.matches("[0-9]"));
            
            String str6 = "Hello world, this is java code.";
            String pattern6 = ".*world.*";
            boolean b6 = Pattern.matches(pattern6, str6);
            System.out.println(b6);
            
            String str7 = "I have a dream.";
            String pattern7 = ".*dream";
            boolean b7 = Pattern.matches(pattern7, str7);
            System.out.println(b7);
            
        }
        
    }
  • 相关阅读:
    supervisor管理airflow
    airflow迁移
    flume部署
    canal原理&部署
    EMR日常操作
    linux的route
    autossh
    Velocity(5)——#macro 指令
    Git(1)----Eclipse安装Git插件
    Velocity(4)——引入指令和#Parse 指令
  • 原文地址:https://www.cnblogs.com/kenantongxue/p/14159715.html
Copyright © 2020-2023  润新知