| 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 | 0 | private static final Perl5Compiler compiler = new Perl5Compiler(); |
| 34 | |
private Pattern pattern; |
| 35 | |
|
| 36 | |
public Perl5RegexpMatcher( String pattern ) { |
| 37 | 0 | this( pattern, false ); |
| 38 | 0 | } |
| 39 | |
|
| 40 | 0 | public Perl5RegexpMatcher( String pattern, boolean multiline ) { |
| 41 | |
try { |
| 42 | 0 | if( multiline ) { |
| 43 | 0 | this.pattern = compiler.compile( pattern, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.MULTILINE_MASK ); |
| 44 | |
} else { |
| 45 | 0 | this.pattern = compiler.compile( pattern, Perl5Compiler.READ_ONLY_MASK ); |
| 46 | |
} |
| 47 | 0 | } catch( MalformedPatternException mpe ) { |
| 48 | 0 | throw new NestableRuntimeException( mpe ); |
| 49 | 0 | } |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
public String getGroupIfMatches( String str, int group ) { |
| 53 | 0 | PatternMatcher matcher = new Perl5Matcher(); |
| 54 | 0 | if( matcher.matches( str, pattern ) ) { |
| 55 | 0 | return matcher.getMatch().group( 1 ); |
| 56 | |
} |
| 57 | 0 | return ""; |
| 58 | |
} |
| 59 | |
|
| 60 | |
public boolean matches( String str ) { |
| 61 | 0 | return new Perl5Matcher().matches( str, pattern ); |
| 62 | |
} |
| 63 | |
} |