apache的Jakarta-ORO库 的正则表达式的使用

[复制链接]
查看11 | 回复3 | 2005-2-28 12:57:00 | 显示全部楼层 |阅读模式
---附件是代码和相关文件
package regularexpressiontest.Jakarta_ORO;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2003
* Company:
* @author wdz :[email protected]
* @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找出第一个数字串&quot

;
containMatch("aaa121-0hhksjds", "\\d+&quot

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

;
containMatch("3$xaaa121-0hhksjds", "[a-z]{4}[0-9]{3}&quot

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

;
preMatch("Catlog catherone cat cat1 catlog catherone", "cat[a-z]*\\s+&quot

;
////找出第一个t*n
System.out.println("ten tig找出第一个t*n&quot

;
containMatch("ten tig", "[a-z]{1}.[a-z]{1}&quot

;
System.out.println("获得年月日&quot

;
getDateString();
// 找出所有 car*的单词,单词分割符号是空格符号或者逗号
System.out.println("找出所有 car*的单词,单词分割符号是空格符号或者逗号&quot

;
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 tonntoon","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-1234ntoon 122-80-7875 434338899","\\d{3}\\-?\\d{2}\\-?\\d{4}",0);
//电话号码
System.out.println("电话号码");
cycleMatch("t023-67890221ntoon023-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 被匹配查找得对想
* @paramreg 匹配规则
* **/
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 被匹配查找得对想
* @paramreg 匹配规则
* **/
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 被匹配查找得对想
* @paramreg 匹配规则
* **/
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();
}
}
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
嘿嘿,以前用过的,他和JDK的正则表达式、perl的正则表达式三者不是太兼容
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
这个值得好好研究的
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
jakarta-oro库 匹配例如((()))这三对括号一样,
就是能从最外层"("开始匹配到最外层")"
下面是模板的置标,自已定义的置标

......

...........


..........



............



........

.......
//====================================================



.......




.......

可以把这块代码看做两大块置标,从外层解析替换配标,要先把一大块匹配取出来
String str0 =

...........


..........



............



........


String str1 =

.......




.......
也就是说能在模板文件里取出从最外层置标的开头到相对应的结尾这块代码匹配替换,

也就是从外到里依次替换
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行