View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004-2007 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.util;
22  
23  import java.util.Vector;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  /**
28   * @author michaellif
29   *
30   */
31  public class RegExUtil {
32  
33      public static String[] match(String input, String regExp) {
34  		Vector resultList = new Vector();
35  
36  		Pattern pattern = Pattern.compile(regExp);
37  		Matcher matcher = pattern.matcher(input);
38  
39  		if (matcher.matches()) {
40  			int groupCount = matcher.groupCount();
41  			for (int i = 1; i <= groupCount; i++) {
42  				resultList.add(matcher.group(i));
43  			}
44  		}
45  
46  		return (String[]) resultList.toArray(new String[0]);
47  	}
48      
49  //	public static String[] match(String input, String regExp) {
50  //		Vector resultList = new Vector();
51  //		org.apache.regexp.RE regex = new org.apache.regexp.RE(regExp);
52  //
53  //		if (regex.match(input)) {
54  //			for (int i = 1; i < regex.getParenCount(); ++i) {
55  //				String s = regex.getParen(i);
56  //				resultList.add(s);
57  //			}
58  //		}
59  //		return (String[]) resultList.toArray(new String[0]);
60  //	}
61  
62      public static String getParen(String input, String regExp) {
63      	Pattern pattern = Pattern.compile(regExp);
64  		Matcher matcher = pattern.matcher(input);
65  		if (matcher.find()) {
66  			return matcher.group(0);
67  		} else {
68  			return null;
69  		}
70      }
71      
72  //    public static String getParen(String input, String regExp) {
73  //    	org.apache.regexp.RE regex = new org.apache.regexp.RE(regExp);
74  //		if (regex.match(input)) {
75  //			return regex.getParen(0);
76  //		}
77  //		return null;
78  //    }
79  }