KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > common > util > RegExUtil


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.common.util;
17
18 import org.apache.oro.text.regex.Pattern;
19 import org.apache.oro.text.regex.Perl5Compiler;
20 import org.apache.oro.text.regex.MalformedPatternException;
21 import org.apache.oro.text.regex.Perl5Substitution;
22 import org.apache.oro.text.regex.Util;
23 import org.apache.oro.text.regex.PatternCompiler;
24 import org.apache.oro.text.regex.Substitution;
25 import org.apache.oro.text.regex.PatternMatcher;
26 import org.apache.oro.text.regex.Perl5Matcher;
27
28
29 /**
30  * <p>Contains methods to operate with strings used in regular expressions and regular expressions themselves
31  * </p>
32  * <p><a HREF="RegExUtil.java.htm"><i>View Source</i></a>
33  * </p>
34  *
35  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
36  * @version $Revision: 1.5 $ $Date: 2005/08/20 10:18:51 $
37  */

38 public class RegExUtil {
39     //~ Static fields/initializers =============================================
40

41     //~ Methods ================================================================
42

43     /**
44      * Escapes regexp metasymbols in given string. Following are escaped:
45      * \, /, ., (, ), [, ], {, }, +, -, *, ?, &, |, ^, $
46      *
47      * @param s the string to be escaped
48      * @return escaped string
49      */

50     public static String JavaDoc escapeMetasymbols(String JavaDoc s) {
51         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
52         for ( int i = 0; i < s.length(); i++ ) {
53             char c = s.charAt(i);
54             switch ( c ) {
55                 case '\\':
56                     sb.append("\\\\");
57                     break;
58                 case '/':
59                     sb.append("\\/");
60                     break;
61                 case '.':
62                     sb.append("\\.");
63                     break;
64                 case '(':
65                     sb.append("\\(");
66                     break;
67                 case ')':
68                     sb.append("\\)");
69                     break;
70                 case '[':
71                     sb.append("\\[");
72                     break;
73                 case ']':
74                     sb.append("\\]");
75                     break;
76                 case '{':
77                     sb.append("\\{");
78                     break;
79                 case '}':
80                     sb.append("\\}");
81                     break;
82                 case '+':
83                     sb.append("\\+");
84                     break;
85                 case '-':
86                     sb.append("\\-");
87                     break;
88                 case '*':
89                     sb.append("\\*");
90                     break;
91                 case '?':
92                     sb.append("\\?");
93                     break;
94                 case '&':
95                     sb.append("\\&");
96                     break;
97                 case '|':
98                     sb.append("\\|");
99                     break;
100                 case '^':
101                     sb.append("\\^");
102                     break;
103                 case '$':
104                     sb.append("\\$");
105                     break;
106                 default:
107                     sb.append(c);
108             }
109         }
110         return sb.toString();
111     }
112
113     /**
114      * Replaces all occurences, which match to given regular expression, in the input by specified replacement string.
115      * @param input Input string to replace occurences in
116      * @param regEx Pattern to select substring to replace
117      * @param replacement String to replace substrings, which match given regular expression
118      * @return String with all occurences, which match given regular expression, replaced with corresponding replacement string
119      * @throws MalformedPatternException if specified regular expression is malformed
120      */

121     public static String JavaDoc replaceAll(String JavaDoc input, String JavaDoc regEx, String JavaDoc replacement) throws MalformedPatternException {
122         return replaceAll(input, regEx, replacement, Perl5Compiler.DEFAULT_MASK);
123     }
124
125     /**
126      * Replaces all occurences, which match to given regular expression, in the input by specified replacement string.
127      * @param input Input string to replace occurences in
128      * @param regEx Pattern to select substring to replace
129      * @param replacement String to replace substrings, which match given regular expression
130      * @return String with all occurences, which match given regular expression, replaced with corresponding replacement string
131      * @throws MalformedPatternException if specified regular expression is malformed
132      */

133     public static String JavaDoc replaceAll(String JavaDoc input, String JavaDoc regEx, String JavaDoc replacement, int options) throws MalformedPatternException {
134         PatternCompiler compiler = new Perl5Compiler();
135         PatternMatcher matcher = new Perl5Matcher();
136         Pattern pattern = compiler.compile(regEx, options);
137         Substitution substitution = new Perl5Substitution(replacement);
138         return Util.substitute(matcher, pattern, substitution, input, Util.SUBSTITUTE_ALL);
139     }
140
141 }
142
Popular Tags