简介
Jakarta-ORO是最全面以及优化得最好的正则表达式API之一,Jakarta-ORO库以前叫做OROMatcher,是由Daniel F. Savarese编写,后来他赠给Jakarta Project。
Jakarta-ORO正则表达式库支持Perl5兼容的正则表达式语法。
环境 jdk1.5.0_12, jakarta-oro-2.0.8
- public static void simpleContains() throws Exception {
- Pattern pattern = new Perl5Compiler().compile( "//d+" );
- Perl5Matcher matcher = new Perl5Matcher();
- PatternMatcherInput matcherInput = new PatternMatcherInput( "北京2008年8月08日20时" );
- while (matcher.contains(matcherInput, pattern)) {
- MatchResult result = matcher.getMatch();
- System.out.println(result.toString());
- }
- }
输出:
2008
8
08
20
- public static void simpleResults() throws Exception {
- Pattern pattern = new Perl5Compiler().compile( "(//d+//.//d+//.//d+//.//d+)@(//d{2}///d{2}///d{4})" );
- Perl5Matcher matcher = new Perl5Matcher();
- PatternMatcherInput matcherInput = new PatternMatcherInput( "202.108.9.38@08/10/2008" );
- while (matcher.contains(matcherInput, pattern)) {
- MatchResult result = matcher.getMatch();
- for ( int i = 0 ; i < result.groups(); i++) {
- System.out.printf("%s : %s/n" , i, result.group(i));
- }
- }
- }
输出:
0 : 202.108.9.38@08/10/2008
1 : 202.108.9.38
2 : 08/10/2008