1 package org.apache.oro.text.regex; 2 3 59 import java.util.*; 60 61 93 public final class Util { 94 99 public static final int SUBSTITUTE_ALL = -1; 100 101 106 public static final int SPLIT_ALL = 0; 107 108 112 private Util() { } 113 114 157 public static Vector split(PatternMatcher matcher, Pattern pattern, 158 String input, int limit) 159 { 160 int beginOffset; 161 Vector results = new Vector(20); 162 MatchResult currentResult; 163 PatternMatcherInput pinput; 164 165 pinput = new PatternMatcherInput(input); 166 beginOffset = 0; 167 168 while(--limit != 0 && matcher.contains(pinput, pattern)) { 169 currentResult = matcher.getMatch(); 170 results.addElement(input.substring(beginOffset, 171 currentResult.beginOffset(0))); 172 beginOffset = currentResult.endOffset(0); 173 } 174 results.addElement(input.substring(beginOffset, input.length())); 175 176 return results; 177 } 178 179 180 213 public static Vector split( PatternMatcher matcher, Pattern pattern, 214 String input) 215 { 216 return split(matcher, pattern, input, SPLIT_ALL); 217 } 218 219 220 241 public static String substitute(PatternMatcher matcher, Pattern pattern, 242 Substitution sub, String input, int numSubs) 243 { 244 int beginOffset, subCount; 245 MatchResult currentResult; 246 PatternMatcherInput pinput; 247 StringBuffer buffer = new StringBuffer (input.length()); 248 249 pinput = new PatternMatcherInput(input); 250 beginOffset = subCount = 0; 251 252 while(numSubs != 0 && matcher.contains(pinput, pattern)) { 255 --numSubs; 256 ++subCount; 257 currentResult = matcher.getMatch(); 258 buffer.append(input.substring(beginOffset, 259 currentResult.beginOffset(0))); 260 sub.appendSubstitution(buffer, currentResult, subCount, 261 input, matcher, pattern); 262 beginOffset = currentResult.endOffset(0); 263 } 264 265 if(subCount == 0) 268 return input; 269 270 buffer.append(input.substring(beginOffset, input.length())); 271 272 return buffer.toString(); 273 } 274 275 294 public static String substitute(PatternMatcher matcher, Pattern pattern, 295 Substitution sub, String input) 296 { 297 return substitute(matcher, pattern, sub, input, 1); 298 } 299 300 } 301 | Popular Tags |