apache的JakartaORO库 的正则表达式的使用
附件是代码和相关文件
package regularexpressiontestJakarta_ORO;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) </p>
* <p>Company: </p>
* @author wdz :
* @version
*/
import orgapacheoroio*;
import orgapacheorotextregex*;
public class Jakarta_OROTest {
public Jakarta_OROTest() {
Systemoutprintln(aaahhksjds找出第一个数字串);
containMatch(aaahhksjds \\d+);
Systemoutprintln(从 $xaaahhksjds 找出第一个[az]{}[]{});
containMatch($xaaahhksjds [az]{}[]{});
Systemoutprintln(从 Catlog catherone cat cat catlog catherone 找出第一个cat[az]*\\s+);
preMatch(Catlog catherone cat cat catlog catherone cat[az]*\\s+);
////找出第一个t*n
Systemoutprintln(ten tig找出第一个t*n);
containMatch(ten tig [az]{}[az]{});
Systemoutprintln(获得年月日);
getDateString();
// 找出所有 car*的单词单词分割符号是空格符号或者逗号
Systemoutprintln(找出所有 car*的单词单词分割符号是空格符号或者逗号);
cycleMatch(Catlog catherone cat cat catlog catlog catherone ((cat\\w*))\\s+);
//找出所有的 扩号内的内容
//使用 (( 和))配对使用可以进行分组
Systemoutprintln(找出所有的 扩号内的内容);
cycleMatch(Cuid=(guest) gid=(others) groups=(users)(floppy) [(]{}((\\w*))[)]{});
//找出所有的日期字符串得月份
//使用 (( 和))配对使用可以进行分组
Systemoutprintln(找出所有的日期字符串的月份);
cycleMatch(July bbb dfg*fg October (([az]{}))\\s[]{}[\\s]?[]{});
//找出所有的 t*n
Systemoutprintln(找出所有的 [a|e|i|o|n]{}n);
cycleMatch(tan ten tin tonn toont[a|e|i|o|n]{}n);
//找出所有的 t*n
// 用于站位想当于文件查找得?符号
Systemoutprintln(找出所有的 t*n);
cycleMatch(tan ten tintonn toon((tn))[\\s|]?);
//和形式的社会安全号码
Systemoutprintln(和形式的社会安全号码);
cycleMatch(tn toon \\d{}\\?\\d{}\\?\\d{});
//电话号码
Systemoutprintln(电话号码);
cycleMatch(tn toon \\d{}\\?\\d{});
//ip列表
Systemoutprin猀??獡????у耈ф?慰档????扩牥虑整?传??????????????????????????????????????????佄???偁??????????????侰?????????偁????????????楈敢湲瑡???????????????????佄??????????????????楈敢湲瑡?????????????????俄????扩牥虑整?????????????????????佄?溜????????????????楈敢湲瑡??????????????????????楈敢湲瑡??????????????????????????????????????????????????????????????????扩牥虑整?????????????楈敢湲瑡???????????????????????????????????????????????????????癡湩??????????????扩牥虑整????????楈敢湲瑡???????????????????扩牥虑整????????????????????????????????????????????扩牥虑整??????????????????????????????????????????????????????????溜??????????????楈敢湲瑡??????????????????扩牥虑整?????????六???????????????楈敢湲瑡???????????????????????????????????????????扩牥虑整???????????????????????????????????????????????????????????????扩牥虑整??????瑨灴?栯扩牥虑整昮湡怂?潣???????扩牥虑整???????????a???tln(ip list 获得ip列表);
cycleMatch(ip list \\d{}\\\\d{}\\\\d{}\\\\d{});
}
/***
*
* 获得年月日
* 例如 June
* */
private void getDateString() {
Systemoutprintln(获得年月日 从dsds June ksdjks 找出第一个日期);
containMatch( dsds June ksdjks
[az]+\\s[]{}\\s[]{});
Systemoutprintln(获得年月日 从June asdsds June ksdjks 找出第一个日期);
containMatch(June asdsds June ksdjks
[\\s]?[az]+\\s[]{}\\s[]{});
}
/***
* 前缀方式的匹配
* @param inputValue 被匹配查找得对想
* @param reg 匹配规则
* **/
private void preMatch(String inputValue String reg) {
PatternCompiler compiler = new PerlCompiler();
PatternMatcher matcher = null;
Pattern pattern = null;
String input = inputValue;
String regexp = reg;
try {
pattern = pile(regexp PerlCompilerCASE_INSENSITIVE_MASK);
matcher = new PerlMatcher();
if (matchermatchesPrefix(input pattern)) {
MatchResult result = matchergetMatch();
Systemoutprintln(result = + resultgroup());
//Systemoutprintln(result =+resultgroup());
}
}
catch (MalformedPatternException e) {
Systemerrprintln(preMatchBad pattern);
Systemerrprintln(egetMessage());
Systemexit();
}
}
/***
* 包含方式的匹配
* @param inputValue 被匹配查找得对想
* @param reg 匹配规则
* **/
private void containMatch(String inputValue String reg) {
// Systemoutprintln(containMatch);
PatternCompiler compiler = new PerlCompiler();
PatternMatcher matcher = null;
Pattern pattern = null;
String input = inputValue;
String regexp = reg;
try {
pattern = pile(regexp PerlCompilerCASE_INSENSITIVE_MASK);
matcher = new PerlMatcher();
if (ntains(input pattern)) {
MatchResult result = matchergetMatch();
Systemoutprintln(result = + resultgroup());
// Systemoutprintln(result =+resultgroup());
}
}
catch (MalformedPatternException e) {
Systemerrprintln(containMatch Bad pattern);猀??獡????
Systemerrprintln(egetMessage());
Systemexit();
}
}
/***
* 循环方式的匹配
* 使用 (( 和))配对使用可以进行分组
* @param inputValue 被匹配查找得对想
* @param reg 匹配规则
* **/
private void cycleMatch(String inputValue String regfinal int groupid){
orgapacheorotextregexPatternCompiler compile = new PerlCompiler();
try {
Pattern p = pile(regPerlCompilerCASE_INSENSITIVE_MASK);
PatternMatcherInput input = new PatternMatcherInput(inputValue);
orgapacheorotextregexPerlMatcher pm = new PerlMatcher();
MatchResult result =null;
int i=;
while(ntains(inputp)){
result = pmgetMatch();
Systemoutprintln(result = + resultgroup(groupid));
inputsetBeginOffset(resultlength());
i++;
}
Systemoutprintln(总共匹配+i+次);
}
catch (Exception ex) {
Systemerrprintln(循环方式的匹配发生错误+exgetMessage());
}
}
public static void main(String[] args) {
Jakarta_OROTest jakarta_OROTest = new Jakarta_OROTest();
}
}