当前位置:   article > 正文

使用Jakarta-ORO库的几个例子

jakartaoroevaluator,

http://oaklet.iteye.com/blog/232969

Jakarta-ORO是最全面以及优化得最好的正则表达式API之一,Jakarta-ORO库以前叫做OROMatcher,是由Daniel F. Savarese编写,后来他赠给Jakarta Project。
    Jakarta-ORO正则表达式库支持Perl5兼容的正则表达式语法。
    环境 jdk1.5.0_12, jakarta-oro-2.0.8

查找:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20simpleContains()%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20%20Pattern%20pattern%20%3D%20new%20Perl5Compiler().compile(%22%5C%5Cd%2B%22)%3B%0A%20%20%20%20%20%20%20%20Perl5Matcher%20matcher%20%3D%20new%20Perl5Matcher()%3B%0A%20%20%20%20%20%20%20%20PatternMatcherInput%20matcherInput%20%3D%20new%20PatternMatcherInput(%22%E5%8C%97%E4%BA%AC2008%E5%B9%B48%E6%9C%8808%E6%97%A520%E6%97%B6%22)%3B%0A%20%20%20%20%20%20%20%20while%20(matcher.contains(matcherInput%2C%20pattern))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20MatchResult%20result%20%3D%20matcher.getMatch()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(result.toString())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  simpleContains()  throws  Exception {  
  2.     Pattern pattern = new  Perl5Compiler().compile( "\\d+" );  
  3.     Perl5Matcher matcher = new  Perl5Matcher();  
  4.     PatternMatcherInput matcherInput = new  PatternMatcherInput( "北京2008年8月08日20时" );  
  5.     while  (matcher.contains(matcherInput, pattern)) {  
  6.         MatchResult result = matcher.getMatch();  
  7.         System.out.println(result.toString());  
  8.     }  
  9. }  
  1. public static void simpleContains() throws Exception {
  2. Pattern pattern = new Perl5Compiler().compile("\\d+");
  3. Perl5Matcher matcher = new Perl5Matcher();
  4. PatternMatcherInput matcherInput = new PatternMatcherInput("北京2008年8月08日20时");
  5. while (matcher.contains(matcherInput, pattern)) {
  6. MatchResult result = matcher.getMatch();
  7. System.out.println(result.toString());
  8. }
  9. }


输出:
2008
8
08
20


分组:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20simpleResults()%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20%20Pattern%20pattern%20%3D%20new%20Perl5Compiler().compile(%22(%5C%5Cd%2B%5C%5C.%5C%5Cd%2B%5C%5C.%5C%5Cd%2B%5C%5C.%5C%5Cd%2B)%40(%5C%5Cd%7B2%7D%2F%5C%5Cd%7B2%7D%2F%5C%5Cd%7B4%7D)%22)%3B%0A%20%20%20%20%20%20%20%20Perl5Matcher%20matcher%20%3D%20new%20Perl5Matcher()%3B%0A%20%20%20%20%20%20%20%20PatternMatcherInput%20matcherInput%20%3D%20new%20PatternMatcherInput(%22202.108.9.38%4008%2F10%2F2008%22)%3B%0A%20%20%20%20%20%20%20%20while%20(matcher.contains(matcherInput%2C%20pattern))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20MatchResult%20result%20%3D%20matcher.getMatch()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20for(int%20i%20%3D%200%3B%20i%20%3C%20result.groups()%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.printf(%22%25s%20%3A%20%25s%5Cn%22%2C%20i%2C%20result.group(i))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  simpleResults()  throws  Exception {  
  2.     Pattern pattern = new  Perl5Compiler().compile( "(\\d+\\.\\d+\\.\\d+\\.\\d+)@(\\d{2}/\\d{2}/\\d{4})" );  
  3.     Perl5Matcher matcher = new  Perl5Matcher();  
  4.     PatternMatcherInput matcherInput = new  PatternMatcherInput( "202.108.9.38@08/10/2008" );  
  5.     while  (matcher.contains(matcherInput, pattern)) {  
  6.         MatchResult result = matcher.getMatch();  
  7.         for ( int  i =  0 ; i < result.groups(); i++) {  
  8.             System.out.printf("%s : %s\n" , i, result.group(i));  
  9.         }  
  10.     }  
  11. }  
  1. public static void simpleResults() throws Exception {
  2. Pattern pattern = new Perl5Compiler().compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)@(\\d{2}/\\d{2}/\\d{4})");
  3. Perl5Matcher matcher = new Perl5Matcher();
  4. PatternMatcherInput matcherInput = new PatternMatcherInput("202.108.9.38@08/10/2008");
  5. while (matcher.contains(matcherInput, pattern)) {
  6. MatchResult result = matcher.getMatch();
  7. for(int i = 0; i < result.groups(); i++) {
  8. System.out.printf("%s : %s\n", i, result.group(i));
  9. }
  10. }
  11. }


输出:
0 : 202.108.9.38@08/10/2008
1 : 202.108.9.38
2 : 08/10/2008


拆分:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20spiltIt()%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20%20String%20rawStr%20%3D%20%22%E5%8C%97%E4%BA%AC%3B%E6%9C%9D%E9%98%B3%3B%E9%B8%9F%E5%B7%A2%E5%A5%A5%E8%BF%90%E4%BC%9A%E5%9C%BA%22%3B%0A%20%20%20%20%20%20%20%20ArrayList%3CString%3E%20results%20%3D%20new%20ArrayList%3CString%3E()%3B%0A%20%20%20%20%20%20%20%20Perl5Matcher%20matcher%20%3D%20new%20Perl5Matcher()%3B%0A%20%20%20%20%20%20%20%20Pattern%20pattern%20%3D%20new%20Perl5Compiler().compile(%22%3B%22)%3B%0A%20%20%20%20%20%20%20%20Util.split(results%2C%20matcher%2C%20pattern%2C%20rawStr)%3B%0A%20%20%20%20%20%20%20%20for%20(String%20r%20%3A%20results)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(r)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  spiltIt()  throws  Exception {  
  2.     String rawStr = "北京;朝阳;鸟巢奥运会场" ;  
  3.     ArrayList<String> results = new  ArrayList<String>();  
  4.     Perl5Matcher matcher = new  Perl5Matcher();  
  5.     Pattern pattern = new  Perl5Compiler().compile( ";" );  
  6.     Util.split(results, matcher, pattern, rawStr);  
  7.     for  (String r : results) {  
  8.         System.out.println(r);  
  9.     }  
  10. }  
  1. public static void spiltIt() throws Exception {
  2. String rawStr = "北京;朝阳;鸟巢奥运会场";
  3. ArrayList<String> results = new ArrayList<String>();
  4. Perl5Matcher matcher = new Perl5Matcher();
  5. Pattern pattern = new Perl5Compiler().compile(";");
  6. Util.split(results, matcher, pattern, rawStr);
  7. for (String r : results) {
  8. System.out.println(r);
  9. }
  10. }


输出:
北京
朝阳
鸟巢奥运会场


替换:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20substituteIt()%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20%20String%20rawStr%20%3D%20%222008-08-11%2017%3A16%3A32%22%3B%0A%20%20%20%20%20%20%20%20Perl5Matcher%20matcher%20%3D%20new%20Perl5Matcher()%3B%0A%20%20%20%20%20%20%20%20Pattern%20pattern%20%3D%20new%20Perl5Compiler().compile(%22-%22)%3B%0A%20%20%20%20%20%20%20%20String%20result%20%3D%20Util.substitute(matcher%2C%20pattern%2C%20new%20Perl5Substitution(%22%2C%22)%2C%20rawStr%2C%20Util.SUBSTITUTE_ALL)%3B%0A%20%20%20%20%20%20%20%20System.out.println(result)%3B%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  substituteIt()  throws  Exception {  
  2.     String rawStr = "2008-08-11 17:16:32" ;  
  3.     Perl5Matcher matcher = new  Perl5Matcher();  
  4.     Pattern pattern = new  Perl5Compiler().compile( "-" );  
  5.     String result = Util.substitute(matcher, pattern, new  Perl5Substitution( "," ), rawStr, Util.SUBSTITUTE_ALL);  
  6.     System.out.println(result);  
  7. }  
  1. public static void substituteIt() throws Exception {
  2. String rawStr = "2008-08-11 17:16:32";
  3. Perl5Matcher matcher = new Perl5Matcher();
  4. Pattern pattern = new Perl5Compiler().compile("-");
  5. String result = Util.substitute(matcher, pattern, new Perl5Substitution(","), rawStr, Util.SUBSTITUTE_ALL);
  6. System.out.println(result);
  7. }


输出:
2008,08,11 17:16:32

替换2:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20substituteIt2()%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20%20String%20rawStr%20%3D%20%222008-08-11%2017%3A16%3A32%22%3B%0A%20%20%20%20%20%20%20%20Perl5Matcher%20matcher%20%3D%20new%20Perl5Matcher()%3B%0A%20%20%20%20%20%20%20%20Pattern%20pattern%20%3D%20new%20Perl5Compiler().compile(%22(%5C%5Cd%7B4%7D)-(%5C%5Cd%7B2%7D)-(%5C%5Cd%7B2%7D)%20(%5C%5Cd%7B2%7D%3A%5C%5Cd%7B2%7D%3A%5C%5Cd%7B2%7D)%22)%3B%0A%20%20%20%20%20%20%20%20String%20result%20%3D%20Util.substitute(matcher%2C%20pattern%2C%20new%20Perl5Substitution(%22%E5%8F%98%E6%8D%A2%20%243%2C%242%2C%241%20%244%22)%2C%20rawStr%2C%20Util.SUBSTITUTE_ALL)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22%E6%A0%BC%E5%BC%8Fyyyy-MM-dd%20HH%3Amm%3Ass%E5%88%B0dd%2CMM%2Cyyyy%20HH%3Amm%3Ass%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(result)%3B%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  substituteIt2()  throws  Exception {  
  2.     String rawStr = "2008-08-11 17:16:32" ;  
  3.     Perl5Matcher matcher = new  Perl5Matcher();  
  4.     Pattern pattern = new  Perl5Compiler().compile( "(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2}:\\d{2})" );  
  5.     String result = Util.substitute(matcher, pattern, new  Perl5Substitution( "变换 $3,$2,$1 $4" ), rawStr, Util.SUBSTITUTE_ALL);  
  6.     System.out.println("格式yyyy-MM-dd HH:mm:ss到dd,MM,yyyy HH:mm:ss" );  
  7.     System.out.println(result);  
  8. }  
  1. public static void substituteIt2() throws Exception {
  2. String rawStr = "2008-08-11 17:16:32";
  3. Perl5Matcher matcher = new Perl5Matcher();
  4. Pattern pattern = new Perl5Compiler().compile("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2}:\\d{2})");
  5. String result = Util.substitute(matcher, pattern, new Perl5Substitution("变换 $3,$2,$1 $4"), rawStr, Util.SUBSTITUTE_ALL);
  6. System.out.println("格式yyyy-MM-dd HH:mm:ss到dd,MM,yyyy HH:mm:ss");
  7. System.out.println(result);
  8. }


输出:
格式yyyy-MM-dd HH:mm:ss到dd,MM,yyyy HH:mm:ss
变换 11,08,2008 17:16:32


小结:
上面的几种用法看起来要比java.util.regex包用着要复杂,如果是简单应用的话还是用自带的包省力。
下面几种用法是Perl5格式用法,如果习惯这种格式,用起来还是比较爽的。

Perl5Util查找格式:
[ m ] / pattern / [ i ][ m ][ s ][ x ]
第一个m写不写都可以(The m prefix is optional)
i  忽略大小写(case insensitive match)
m  多行模式(treat the input as consisting of multiple lines)
s  单行模式(treat the input as consisting of a single line)
x  使用扩展语法 (enable extended expression syntax)
查找  MyDoc/Java/SimpleJava  时
正常写法  "m/MyDoc\\/Java\\/SimpleJava/"
扩展写法  "m#MyDoc/Java/SimpleJava#x"

Perl5Util查找:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20perl5Match()%20%7B%0A%20%20%20%20%20%20%20%20Perl5Util%20plUtil%20%3D%20new%20Perl5Util()%3B%0A%20%20%20%20%20%20%20%20PatternMatcherInput%20matcherInput%20%3D%20new%20PatternMatcherInput(%22%E5%8C%97%E4%BA%AC2008%E5%B9%B48%E6%9C%8808%E6%97%A520%E6%97%B6%22)%3B%0A%20%20%20%20%20%20%20%20while%20(plUtil.match(%22%2F%5C%5Cd%2B%2F%22%2C%20matcherInput))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20MatchResult%20result%20%3D%20plUtil.getMatch()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(result.toString())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  perl5Match() {  
  2.     Perl5Util plUtil = new  Perl5Util();  
  3.     PatternMatcherInput matcherInput = new  PatternMatcherInput( "北京2008年8月08日20时" );  
  4.     while  (plUtil.match( "/\\d+/" , matcherInput)) {  
  5.         MatchResult result = plUtil.getMatch();  
  6.         System.out.println(result.toString());  
  7.     }  
  8. }  
  1. public static void perl5Match() {
  2. Perl5Util plUtil = new Perl5Util();
  3. PatternMatcherInput matcherInput = new PatternMatcherInput("北京2008年8月08日20时");
  4. while (plUtil.match("/\\d+/", matcherInput)) {
  5. MatchResult result = plUtil.getMatch();
  6. System.out.println(result.toString());
  7. }
  8. }


输出:
2008
8
08
20


分组:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20perl5Match2()%20%7B%0A%20%20%20%20%20%20%20%20Perl5Util%20plUtil%20%3D%20new%20Perl5Util()%3B%0A%20%20%20%20%20%20%20%20PatternMatcherInput%20matcherInput%20%3D%20new%20PatternMatcherInput(%22202.108.9.38%4008%2F10%2F2008%22)%3B%0A%20%20%20%20%20%20%20%20while%20(plUtil.match(%22m%2308(%2F10%2F)2008%23x%22%2C%20matcherInput))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20MatchResult%20result%20%3D%20plUtil.getMatch()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.printf(%22%25s%20%3A%20%25s%5Cn%22%2C%200%2C%20result.group(0))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.printf(%22%25s%20%3A%20%25s%5Cn%22%2C%201%2C%20result.group(1))%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  perl5Match2() {  
  2.     Perl5Util plUtil = new  Perl5Util();  
  3.     PatternMatcherInput matcherInput = new  PatternMatcherInput( "202.108.9.38@08/10/2008" );  
  4.     while  (plUtil.match( "m#08(/10/)2008#x" , matcherInput)) {  
  5.         MatchResult result = plUtil.getMatch();  
  6.         System.out.printf("%s : %s\n"0 , result.group( 0 ));  
  7.         System.out.printf("%s : %s\n"1 , result.group( 1 ));  
  8.     }  
  9. }  
  1. public static void perl5Match2() {
  2. Perl5Util plUtil = new Perl5Util();
  3. PatternMatcherInput matcherInput = new PatternMatcherInput("202.108.9.38@08/10/2008");
  4. while (plUtil.match("m#08(/10/)2008#x", matcherInput)) {
  5. MatchResult result = plUtil.getMatch();
  6. System.out.printf("%s : %s\n", 0, result.group(0));
  7. System.out.printf("%s : %s\n", 1, result.group(1));
  8. }
  9. }


输出:
0 : 08/10/2008
1 : /10/


Perl5Util替换格式:
s / pattern / replacement / [ g ][ i ][ m ][ o ][ s ][ x ]
第一个s必须要写(The s prefix is mandatory)
g 全部替换。默认只替换第一个
i  忽略大小写(case insensitive match)
m  多行模式(treat the input as consisting of multiple lines)
o   只替换第一个(吾不确认)
s  单行模式(treat the input as consisting of a single line)
x  使用扩展语法 (enable extended expression syntax)

Perl5Util替换:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20perl5Substitute()%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20%20String%20rawStr%20%3D%20%222008-08-11%2017%3A16%3A32%22%3B%0A%20%20%20%20%20%20%20%20Perl5Util%20plutil%20%3D%20new%20Perl5Util()%3B%0A%20%20%20%20%20%20%20%20String%20result%20%3D%20plutil.substitute(%22s%2F-%2F%2C%2Fg%22%2C%20rawStr)%3B%0A%20%20%20%20%20%20%20%20System.out.println(result)%3B%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  perl5Substitute()  throws  Exception {  
  2.     String rawStr = "2008-08-11 17:16:32" ;  
  3.     Perl5Util plutil = new  Perl5Util();  
  4.     String result = plutil.substitute("s/-/,/g" , rawStr);  
  5.     System.out.println(result);  
  6. }  
  1. public static void perl5Substitute() throws Exception {
  2. String rawStr = "2008-08-11 17:16:32";
  3. Perl5Util plutil = new Perl5Util();
  4. String result = plutil.substitute("s/-/,/g", rawStr);
  5. System.out.println(result);
  6. }


输出:
2008,08,11 17:16:32

Perl5Util替换2:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20perl5Substitute2()%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20%20String%20rawStr%20%3D%20%222008-08-11%2017%3A16%3A32%22%3B%0A%20%20%20%20%20%20%20%20Perl5Util%20plutil%20%3D%20new%20Perl5Util()%3B%0A%20%20%20%20%20%20%20%20String%20result%20%3D%20plutil.substitute(%22s%2F(%5C%5Cd%7B4%7D)-(%5C%5Cd%7B2%7D)-(%5C%5Cd%7B2%7D)%20(%5C%5Cd%7B2%7D%3A%5C%5Cd%7B2%7D%3A%5C%5Cd%7B2%7D)%2F%E5%8F%98%E6%8D%A2%20%243%2C%242%2C%241%20%244%2Fg%22%2C%20rawStr)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22%E6%A0%BC%E5%BC%8Fyyyy-MM-dd%20HH%3Amm%3Ass%E5%88%B0dd%2CMM%2Cyyyy%20HH%3Amm%3Ass%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(result)%3B%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  perl5Substitute2()  throws  Exception {  
  2.     String rawStr = "2008-08-11 17:16:32" ;  
  3.     Perl5Util plutil = new  Perl5Util();  
  4.     String result = plutil.substitute("s/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2}:\\d{2})/变换 $3,$2,$1 $4/g" , rawStr);  
  5.     System.out.println("格式yyyy-MM-dd HH:mm:ss到dd,MM,yyyy HH:mm:ss" );  
  6.     System.out.println(result);  
  7. }  
  1. public static void perl5Substitute2() throws Exception {
  2. String rawStr = "2008-08-11 17:16:32";
  3. Perl5Util plutil = new Perl5Util();
  4. String result = plutil.substitute("s/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2}:\\d{2})/变换 $3,$2,$1 $4/g", rawStr);
  5. System.out.println("格式yyyy-MM-dd HH:mm:ss到dd,MM,yyyy HH:mm:ss");
  6. System.out.println(result);
  7. }


输出:
格式yyyy-MM-dd HH:mm:ss到dd,MM,yyyy HH:mm:ss
变换 11,08,2008 17:16:32


Perl5Util替换3:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20perl5AddCommas()%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20%20String%20rawStr%20%3D%20%22314159265%22%3B%0A%20%20%20%20%20%20%20%20Perl5Util%20plutil%20%3D%20new%20Perl5Util()%3B%0A%20%20%20%20%20%20%20%20while(plutil.match(%22%2F%5B%2B-%5D%3F%5C%5Cd*%5C%5Cd%7B4%7D%2F%22%2C%20rawStr))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20rawStr%20%3D%20plutil.substitute(%22s%2F(%5B%2B-%5D%3F%5C%5Cd*%5C%5Cd)(%5C%5Cd%7B3%7D)%2F%241%2C%242%2F%22%2C%20rawStr)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(rawStr)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20System.out.println(rawStr)%3B%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  perl5AddCommas()  throws  Exception {  
  2.     String rawStr = "314159265" ;  
  3.     Perl5Util plutil = new  Perl5Util();  
  4.     while (plutil.match( "/[+-]?\\d*\\d{4}/" , rawStr)){  
  5.         rawStr = plutil.substitute("s/([+-]?\\d*\\d)(\\d{3})/$1,$2/" , rawStr);  
  6.         System.out.println(rawStr);  
  7.     }  
  8.     System.out.println(rawStr);  
  9. }  
  1. public static void perl5AddCommas() throws Exception {
  2. String rawStr = "314159265";
  3. Perl5Util plutil = new Perl5Util();
  4. while(plutil.match("/[+-]?\\d*\\d{4}/", rawStr)){
  5. rawStr = plutil.substitute("s/([+-]?\\d*\\d)(\\d{3})/$1,$2/", rawStr);
  6. System.out.println(rawStr);
  7. }
  8. System.out.println(rawStr);
  9. }


输出:
314159,265
314,159,265
314,159,265


过滤:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://oaklet.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20public%20static%20void%20filter()%20%7B%0A%20%20%20%20%20%20%20%20String%20path%20%3D%20%22D%3A%5C%5CMyDoc%5C%5CJava%5C%5CSimpleJava%22%3B%0A%20%20%20%20%20%20%20%20File%20file%20%3D%20new%20File(path)%3B%0A%20%20%20%20%20%20%20%20String%5B%5D%20globList%20%3D%20file.list(new%20GlobFilenameFilter(%22*.java%22))%3B%0A%20%20%20%20%20%20%20%20for%20(String%20fn%20%3A%20globList)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(fn)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20String%5B%5D%20perl5List%20%3D%20file.list(new%20Perl5FilenameFilter(%22.%2B%5C%5C.java%22))%3B%0A%20%20%20%20%20%20%20%20for%20(String%20fn%20%3A%20perl5List)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(fn)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   static   void  filter() {  
  2.     String path = "D:\\MyDoc\\Java\\SimpleJava" ;  
  3.     File file = new  File(path);  
  4.     String[] globList = file.list(new  GlobFilenameFilter( "*.java" ));  
  5.     for  (String fn : globList) {  
  6.         System.out.println(fn);  
  7.     }  
  8.     String[] perl5List = file.list(new  Perl5FilenameFilter( ".+\\.java" ));  
  9.     for  (String fn : perl5List) {  
  10.         System.out.println(fn);  
  11.     }  
  12. }  
  1. public static void filter() {
  2. String path = "D:\\MyDoc\\Java\\SimpleJava";
  3. File file = new File(path);
  4. String[] globList = file.list(new GlobFilenameFilter("*.java"));
  5. for (String fn : globList) {
  6. System.out.println(fn);
  7. }
  8. String[] perl5List = file.list(new Perl5FilenameFilter(".+\\.java"));
  9. for (String fn : perl5List) {
  10. System.out.println(fn);
  11. }
  12. }


输出:
DepCreater.java
ReadAndWrite.java
DepCreater.java
ReadAndWrite.java


模式匹配的几个问题:
表达式的递归匹配
正则表达式来分析一个计算式中的括号配对情况,想办法把有嵌套的的成对括号或者成对标签匹配出来。

非贪婪匹配的效率
当一个表达式中,有多个非贪婪匹配时,或者多个未知匹配次数的表达式时,这个表达式将可能存在效率上的陷阱。有时候,匹配速度非常慢。(简单避开方法表达式使用不超过一个非贪婪)

参考文章:
http://xzio.iteye.com/blog/121291

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/892051
推荐阅读
相关标签
  

闽ICP备14008679号