---附件是代码和相关文件 Title:
Description: Copyright:
Copyright (c) 2003 Company:
package
regularexpressiontest.Jakarta_ORO;
/**
*
*
*
*
* @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();
}
}