1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json.regexp;
18
19 import org.apache.commons.lang.exception.NestableRuntimeException;
20 import org.apache.oro.text.regex.MalformedPatternException;
21 import org.apache.oro.text.regex.Pattern;
22 import org.apache.oro.text.regex.PatternMatcher;
23 import org.apache.oro.text.regex.Perl5Compiler;
24 import org.apache.oro.text.regex.Perl5Matcher;
25
26
27
28
29
30
31
32 public class Perl5RegexpMatcher implements RegexpMatcher {
33 private static final Perl5Compiler compiler = new Perl5Compiler();
34 private Pattern pattern;
35
36 public Perl5RegexpMatcher( String pattern ) {
37 this( pattern, false );
38 }
39
40 public Perl5RegexpMatcher( String pattern, boolean multiline ) {
41 try {
42 if( multiline ) {
43 this.pattern = compiler.compile( pattern, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.MULTILINE_MASK );
44 } else {
45 this.pattern = compiler.compile( pattern, Perl5Compiler.READ_ONLY_MASK );
46 }
47 } catch( MalformedPatternException mpe ) {
48 throw new NestableRuntimeException( mpe );
49 }
50 }
51
52 public String getGroupIfMatches( String str, int group ) {
53 PatternMatcher matcher = new Perl5Matcher();
54 if( matcher.matches( str, pattern ) ) {
55 return matcher.getMatch().group( 1 );
56 }
57 return "";
58 }
59
60 public boolean matches( String str ) {
61 return new Perl5Matcher().matches( str, pattern );
62 }
63 }