ITPUB??ì3
ITPUB论坛 » Java企业开发 » apache的Jakarta-ORO库 的正则表达式的使用

新一届的微软MVP评选已经开始,欢迎各位推荐!

标题: [精华] apache的Jakarta-ORO库 的正则表达式的使用
离线 wdzwdz
版主



精华贴数 11
个人空间 0
技术积分 2548 (613)
社区积分 1067 (922)
注册日期 2001-12-30
论坛徽章:10
管理团队成员ITPUB元老管理团队2006纪念徽章会员2007贡献徽章会员2006贡献徽章授权会员
2008北京奥运纪念徽章:柔道ITPUB新首页上线纪念徽章在线时间开发板块每日发贴之星  

发表于 2004-2-15 19:53 
apache的Jakarta-ORO库 的正则表达式的使用

---附件是代码和相关文件
package regularexpressiontest.Jakarta_ORO;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author wdz :  wdz123@hotmail.com
* @version 1.0
*/

import org.apache.oro.io.*;
import org.apache.oro.text.regex.*;

public class Jakarta_OROTest1 {
  public Jakarta_OROTest1() {
    System.out.println("aaa121-0hhksjds找出第一个数字串";
    containMatch("aaa121-0hhksjds", "\\d+";

    System.out.println("从 3$xaaa121-0hhksjds 找出第一个[a-z]{4}[0-9]{3}";
    containMatch("3$xaaa121-0hhksjds", "[a-z]{4}[0-9]{3}";

    System.out.println("从 Catlog catherone cat cat1 catlog catherone 找出第一个cat[a-z]*\\s+";
    preMatch("Catlog catherone cat cat1 catlog catherone", "cat[a-z]*\\s+";

    ////找出第一个t*n
    System.out.println("ten tig找出第一个t*n";
    containMatch("ten tig", "[a-z]{1}.[a-z]{1}";

    System.out.println("获得年月日";
    getDateString();

    // 找出所有 car*的单词,单词分割符号是空格符号或者逗号
    System.out.println("找出所有 car*的单词,单词分割符号是空格符号或者逗号";
    cycleMatch("Catlog catherone cat cat1 catlog catlog2 catherone", "((cat\\w*))\\s+",0);

    //找出所有的 扩号内的内容
    //使用 ((  和))配对使用可以进行分组,
    System.out.println("找出所有的 扩号内的内容");
    cycleMatch("Cuid=100(guest) gid=100(others) groups=10(users),11(floppy)", "[(]{1}((\\w*))[)]{1}",1);

    //找出所有的日期字符串得月份
    //使用 ((  和))配对使用可以进行分组,
    System.out.println("找出所有的日期字符串的月份");
    cycleMatch("July 11, 2003 bbb 423434dfg*fg October 22, 2004", "(([a-z]{1,10}))\\s[0-9]{1,2},[\\s]?[0-9]{4}",1);


    //找出所有的 t*n
   System.out.println("找出所有的 [a|e|i|o|n]{1,2}n");
    cycleMatch("tan ten tin tonn  toon","t[a|e|i|o|n]{1,2}n",0);

    //找出所有的 t*n
    // .用于站位,想当于文件查找得?符号
    System.out.println("找出所有的 t*n");
    cycleMatch("tan ten tin,tonn toon","((t.n))[\\s|,]?",1);

    //123-12-1234和123121234形式的社会安全号码
    System.out.println("123-12-1234和123121234形式的社会安全号码");
    cycleMatch("t199-12-1234n  toon 122-80-7875 434338899","\\d{3}\\-?\\d{2}\\-?\\d{4}",0);
    //电话号码
    System.out.println("电话号码");
    cycleMatch("t023-67890221n  toon023-88890221 4312906677","\\d{3}\\-?\\d{8}",0);

    //ip列表
    System.out.println("ip list --192.168.200.10,192.168.201.11获得ip列表");
    cycleMatch("ip list --192.168.200.10,192.168.211.51","\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}",0);

  }

  /***
   *
   * 获得年月日,
   * 例如 :June 26, 1951
   * */
  private void getDateString() {
     System.out.println("获得年月日 ,从dsds June 26, 1951 ksdjks 找出第一个日期");
    containMatch(" dsds June 26, 1951 ksdjks ",
                 "[a-z]+\\s[0-9]{1,2},\\s[0-9]{4}");
    System.out.println("获得年月日 ,从June 16, 1959 asdsds June 11, 1911 ksdjks 找出第一个日期");
    containMatch("June 16, 1959 asdsds June 11, 1911 ksdjks ",
                 "[\\s]?[a-z]+\\s[0-9]{1,2},\\s[0-9]{4}");
  }

  /***
   * 前缀方式的匹配
   * @param inputValue 被匹配查找得对想
   * @param  reg 匹配规则
   * **/
  private void preMatch(String inputValue, String reg) {
    PatternCompiler compiler = new Perl5Compiler();
    PatternMatcher matcher = null;
    Pattern pattern = null;
    String input = inputValue;
    String regexp = reg;
    try {
      pattern = compiler.compile(regexp, Perl5Compiler.CASE_INSENSITIVE_MASK);
      matcher = new Perl5Matcher();
      if (matcher.matchesPrefix(input, pattern)) {
        MatchResult result = matcher.getMatch();
        System.out.println("result =" + result.group(0));
        //System.out.println("result ="+result.group(1));
      }
    }
    catch (MalformedPatternException e) {
      System.err.println("preMatch--Bad pattern.");
      System.err.println(e.getMessage());
      System.exit(1);
    }
  }

  /***
   * 包含方式的匹配
   * @param inputValue 被匹配查找得对想
   * @param  reg 匹配规则
   * **/
  private void containMatch(String inputValue, String reg) {
   // System.out.println("containMatch----");
    PatternCompiler compiler = new Perl5Compiler();
    PatternMatcher matcher = null;
    Pattern pattern = null;
    String input = inputValue;
    String regexp = reg;
    try {
      pattern = compiler.compile(regexp, Perl5Compiler.CASE_INSENSITIVE_MASK);
      matcher = new Perl5Matcher();
      if (matcher.contains(input, pattern)) {
        MatchResult result = matcher.getMatch();
        System.out.println("result =" + result.group(0));
        // System.out.println("result ="+result.group(1));
      }
    }
    catch (MalformedPatternException e) {
      System.err.println("containMatch ---Bad pattern.");
      System.err.println(e.getMessage());
      System.exit(1);
    }
  }
  /***
   * 循环方式的匹配
   * 使用 ((  和))配对使用可以进行分组
   * @param inputValue 被匹配查找得对想
   * @param  reg 匹配规则
   * **/
  private void cycleMatch(String inputValue, String reg,final int groupid){
    org.apache.oro.text.regex.PatternCompiler compile = new Perl5Compiler();
    try {
      Pattern p = compile.compile(reg,Perl5Compiler.CASE_INSENSITIVE_MASK);
      PatternMatcherInput input = new PatternMatcherInput(inputValue);
      org.apache.oro.text.regex.Perl5Matcher pm  = new Perl5Matcher();
      MatchResult result =null;
      int i=0;
     while(pm.contains(input,p)){
       result = pm.getMatch();
       System.out.println("result =" + result.group(groupid));
       input.setBeginOffset(result.length());
       i++;
     }
     System.out.println("总共匹配"+i+"次");
    }
    catch (Exception ex) {
       System.err.println("循环方式的匹配发生错误"+ex.getMessage());
    }
  }

  public static void main(String[] args) {
    Jakarta_OROTest1 jakarta_OROTest11 = new Jakarta_OROTest1();
  }

}




wdzwdz 上传了这个附件:
2004-2-15 19:53
  下载次数: 390
regularexpressiontest.rar (346.46 KB)
 
__________________
从事系统分析工作qq=24938558,msn=wdz123@hotmail.com我的bloghttp://blog.itpub.net/wdzwdz
只看该作者    顶部
离线 greenflute


精华贴数 0
个人空间 0
技术积分 7216 (178)
社区积分 162 (2640)
注册日期 2001-10-26
论坛徽章:15
现任管理团队成员ITPUB元老管理团队2007贡献徽章会员2007贡献徽章2008北京奥运纪念徽章:沙滩排球2008年新春纪念徽章
      

发表于 2004-2-16 11:55 
嘿嘿,以前用过的,他和JDK的正则表达式、perl的正则表达式三者不是太兼容


只看该作者    顶部
离线 wqbi
好久不见


精华贴数 3
个人空间 0
技术积分 4493 (304)
社区积分 4645 (303)
注册日期 2004-11-13
论坛徽章:2
会员2006贡献徽章     
      

发表于 2006-5-22 23:57 
这个值得好好研究的


__________________
There should be a better way to start a day than waking up every morning.
只看该作者    顶部
离线 xzliulin
初级会员



精华贴数 0
个人空间 0
技术积分 2 (218473)
社区积分 0 (1529904)
注册日期 2007-8-27
论坛徽章:1
ITPUB新首页上线纪念徽章     
      

发表于 2007-9-7 15:12 
jakarta-oro库

jakarta-oro库 匹配例如((()))这三对括号一样,
就是能从最外层"("开始匹配到最外层")"
下面是模板的置标,自已定义的置标
        ......
<ecms.list name="title">
                ...........
         <ecms.column num="10">
                        ..........
                <ecms.content field="Title"></ecms.content>
                        ............
        </ecms.column>
                ........
</ecms.list>
        .......
//====================================================       
<ecms.column num="10">
                .......       
        <ecms.content field="Title"></ecms.content>
                .......
</ecms.column>

可以把这块代码看做两大块置标,从外层解析替换配标,要先把一大块匹配取出来
String str0 = <ecms.list name="title">
                ...........
         <ecms.column num="10">
                        ..........
                <ecms.content field="Title"></ecms.content>
                        ............
        </ecms.column>
                ........
</ecms.list>



String str1 = <ecms.column num="10">
                .......       
        <ecms.content field="Title"></ecms.content>
                .......
</ecms.column>
也就是说能在模板文件里取出从最外层置标的开头到相对应的结尾这块代码匹配替换,


也就是从外到里依次替换


只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问