zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然 JAVA开发学习:正则表达式

    import java.util.regex.*;
     
    class RegexExample1{
       public static void main(String args[]){
          String content = "I am noob " +
            "from runoob.com.";
     
          String pattern = ".*runoob.*";
     
          boolean isMatch = Pattern.matches(pattern, content);
          System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
       }
    }

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    public class RegexMatches
    {
        public static void main( String args[] ){
     
          // 按指定模式在字符串查找
          String line = "This order was placed for QT3000! OK?";
          String pattern = "(\D*)(\d+)(.*)";
     
          // 创建 Pattern 对象
          Pattern r = Pattern.compile(pattern);
     
          // 现在创建 matcher 对象
          Matcher m = r.matcher(line);
          if (m.find( )) {
             System.out.println("Found value: " + m.group(0) );
             System.out.println("Found value: " + m.group(1) );
             System.out.println("Found value: " + m.group(2) );
             System.out.println("Found value: " + m.group(3) ); 
          } else {
             System.out.println("NO MATCH");
          }
       }
    }

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    public class RegexMatches
    {
        private static final String REGEX = "\bcat\b";
        private static final String INPUT =
                                        "cat cat cat cattie cat";
     
        public static void main( String args[] ){
           Pattern p = Pattern.compile(REGEX);
           Matcher m = p.matcher(INPUT); // 获取 matcher 对象
           int count = 0;
     
           while(m.find()) {
             count++;
             System.out.println("Match number "+count);
             System.out.println("start(): "+m.start());
             System.out.println("end(): "+m.end());
          }
       }
    }

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    public class RegexMatches
    {
        private static final String REGEX = "foo";
        private static final String INPUT = "fooooooooooooooooo";
        private static final String INPUT2 = "ooooofoooooooooooo";
        private static Pattern pattern;
        private static Matcher matcher;
        private static Matcher matcher2;
     
        public static void main( String args[] ){
           pattern = Pattern.compile(REGEX);
           matcher = pattern.matcher(INPUT);
           matcher2 = pattern.matcher(INPUT2);
     
           System.out.println("Current REGEX is: "+REGEX);
           System.out.println("Current INPUT is: "+INPUT);
           System.out.println("Current INPUT2 is: "+INPUT2);
     
     
           System.out.println("lookingAt(): "+matcher.lookingAt());
           System.out.println("matches(): "+matcher.matches());
           System.out.println("lookingAt(): "+matcher2.lookingAt());
       }
    }

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    public class RegexMatches
    {
        private static String REGEX = "dog";
        private static String INPUT = "The dog says meow. " +
                                        "All dogs say meow.";
        private static String REPLACE = "cat";
     
        public static void main(String[] args) {
           Pattern p = Pattern.compile(REGEX);
           // get a matcher object
           Matcher m = p.matcher(INPUT); 
           INPUT = m.replaceAll(REPLACE);
           System.out.println(INPUT);
       }
    }

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    public class RegexMatches
    {
       private static String REGEX = "a*b";
       private static String INPUT = "aabfooaabfooabfoobkkk";
       private static String REPLACE = "-";
       public static void main(String[] args) {
          Pattern p = Pattern.compile(REGEX);
          // 获取 matcher 对象
          Matcher m = p.matcher(INPUT);
          StringBuffer sb = new StringBuffer();
          while(m.find()){
             m.appendReplacement(sb,REPLACE);
          }
          m.appendTail(sb);
          System.out.println(sb.toString());
       }
    }

  • 相关阅读:
    第19篇 2016年计划
    第18篇 我的中国梦
    Linux中文件实时同步
    Ansible Playbook
    Ansible简介及常用模块
    HTTP协议简单认识
    zabbix 分布式监控Proxy
    Zabbix中Agent自动注册
    Groovy基础语法
    Python文件操作
  • 原文地址:https://www.cnblogs.com/tszr/p/10960443.html
Copyright © 2011-2022 走看看