欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用 Java 中的正则表达式检查字符串

最编程 2024-07-12 20:38:32
...
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

/** 
* Created by IntelliJ IDEA. 

* @author leizhimin 2007-6-18 16:00:49 
*/
 
public  class TestPattern { 

         public  static  void main(String args[]) { 
                test1(); 
                test2(); 
                test3(); 
        } 

         public  static  void test1() { 
                 //创建正则表达式的模式对象 
                Pattern p = Pattern.compile( "a*b"); //零次或多次a,并跟着b 
                 //通过模式对象创建一个匹配对象 
                Matcher m1 = p.matcher( "aaaaabxyzabt"); 
                Matcher m2 = p.matcher( "aaaaab"); 
                 //尝试将整个区域与模式匹配。当且仅当整个区域序列匹配此匹配器的模式时才返回 true。 
                 boolean b11 = m1.matches(); 
                 boolean b12 = m2.matches(); 
                System.out.println(b11 +  "\t" + b12); 

                 //编译给定正则表达式并尝试将给定输入与其匹配。 
                 boolean b21 = Pattern.matches( "a*b""aaaaabxyzabt"); 
                 boolean b22 = Pattern.matches( "a*b""aaaaab"); 
                System.out.println(b21 +  "\t" + b22); 

                System.out.println(p.flags()); 
        } 

         /** 
         * 进行大小写忽略和多行模式测试 
         */
 
         public  static  void test2() { 
                Pattern p1 = Pattern.compile( "a*b", Pattern.CASE_INSENSITIVE); 
                Pattern p2 = Pattern.compile( "a*b", Pattern.MULTILINE); 

                Matcher m11 = p1.matcher( "aaaaaBABzAbt"); 
                Matcher m12 = p1.matcher( "Ab"); 
                Matcher m21 = p2.matcher( "a\n" +  "b"); 
                Matcher m22 = p2.matcher( "ab"); 

                 boolean b11 = m11.matches(); 
                 boolean b12 = m12.matches(); 
                System.out.println(b11 +  "\t" + b12); 
                 boolean b21 = m21.matches(); 
                 boolean b22 = m22.matches(); 

                System.out.println(b21 +  "\t" + b22); 
                System.out.println(p1.flags());                              //返回此模式的匹配标志。 
                System.out.println(p1.pattern());                          //返回在其中编译过此模式的正则表达式。 
                System.out.println(Pattern.quote( "aaaaa"));      //返回指定 String 的字面值模式 String。 
                String[] arr1 = p1.split( "xabyayAbt");                 //围绕此模式的匹配拆分给定输入序列。 
                 for (String s : arr1) { 
                        System.out.println(s); 
                } 
                String[] arr2 = p1.split( "xabyayAbt", 2);          //围绕此模式的匹配拆分给定输入序列。 
                 for (String s : arr2) { 
                        System.out.println(s); 
                } 
                System.out.println(p1.toString());                         //返回此模式的字符串表示形式。 
        } 

         public  static  void test3() { 
                Pattern p = Pattern.compile( "cat"); 
                Matcher m = p.matcher( "cat two cats in the yard"); 
                System.out.println(m.groupCount()); 
                System.out.println(m.matches()); 
                System.out.println(m.find()); 
                System.out.println(m.find(8)); 
                StringBuffer sb =  new StringBuffer(); 
                 while (m.find()) { 
                        m.appendReplacement(sb,  "dog"); 
                } 
                m.appendTail(sb); 
                System.out.println(sb.toString()); 

                System.out.println(m.replaceAll( "person")); 
                System.out.println(m.replaceFirst( "person")); 

                 while (m.find()) { 
                        System.out.println(m.group()); 
                } 

        } 
}

推荐阅读