服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

apache的jakarta-oro库 的正则表达式的使用


  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.prin?q????????у?uф?慰档????扩???]整?传????????????????????????????????????????????????????????????????c????????????????????????旄?????????????????????????????????????????????????旄??????????????????????俄????扩???]整????????????????????????溜?????????????????旄????????????????????????????旄???????????????????????????????????????????????????????????????????????扩???]整??????????????旄?????????????????????????????????????????????????????????????v????????????????扩???]整?????????旄????????????????????????扩???]整????????????????????????`???????????????????????????`???扩???]整????????????????????????????????????????????????????????????????????溜?????????????????旄???????????????????????扩???]整?????????六??????????????????旄????????????????????????????????????????????????扩???]整?????????????????????????????????????????????????????????????????扩???]整????????????肜????]整?f???z??????????扩???]整???????????a??? tln("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.");?q????????
  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();
  }
  
  }

扫描关注微信公众号