1 /*
2 * Copyright 2002-2009 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.json.regexp;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 /**
23 * JDK 1.4+ RegexpMatcher implementation.
24 *
25 * @author Andres Almiray <aalmiray@users.sourceforge.net>
26 */
27 public class JdkRegexpMatcher implements RegexpMatcher {
28 private final Pattern pattern;
29
30 public JdkRegexpMatcher( String pattern ) {
31 this( pattern, false );
32 }
33
34 public JdkRegexpMatcher( String pattern, boolean multiline ) {
35 if( multiline ) {
36 this.pattern = Pattern.compile( pattern, Pattern.MULTILINE );
37 } else {
38 this.pattern = Pattern.compile( pattern );
39 }
40 }
41
42 public String getGroupIfMatches( String str, int group ) {
43 Matcher matcher = pattern.matcher( str );
44 if( matcher.matches() ) {
45 return matcher.group( group );
46 }
47 return "";
48 }
49
50 public boolean matches( String str ) {
51 return pattern.matcher( str ).matches();
52 }
53 }