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());
       }
    }

  • 相关阅读:
    关于CSDN指针讨论的心得
    VC++ 6.0 与VS2008 C++ DEBUG工具(Windows)介绍
    VC++ 申明静态变量的注意事项
    大家好,我是新的blue1000~
    [讨论]当我采用动态sql绑定datagrid分页的时候,遇到的问题
    我与Google有个对话
    [BK专访]一切以客户为中心,其它一切纷至沓来
    [译]在.net中使用GDI+来提高gif图片的保存画质
    微软能不能别这样坑爹啊,即使不中毒,也伤不起啊
    长见识!1021字节javascript写成的3D圣诞树
  • 原文地址:https://www.cnblogs.com/tszr/p/10960443.html
Copyright © 2011-2022 走看看