1 20 21 package com.tonicsystems.jarjar.regex; 22 23 import gnu.regexp.RE; 24 import gnu.regexp.REException; 25 import gnu.regexp.REMatch; 26 27 public class GnuRegexEngine implements RegexEngine 28 { 29 public Pattern compile(String pattern) { 30 try { 31 final RE re = new RE(pattern); 32 return new Pattern() { 33 public String replaceAll(String value, String replace) { 34 return re.substituteAll(value, replace, 0, RE.REG_NO_INTERPOLATE); 35 } 36 public boolean matches(String value) { 37 return re.isMatch(value); 38 } 39 public int groupCount() { 40 return re.getNumSubs(); 41 } 42 public Matcher getMatcher(final String value) { 43 final REMatch match = re.getMatch(value, 0); 44 return new Matcher() { 45 public boolean matches() { 46 return re.isMatch(value); } 48 public int start() { 49 return match.getStartIndex(); 50 } 51 public int end() { 52 return match.getEndIndex(); 53 } 54 public String group(int index) { 55 return match.toString(index); 56 } 57 }; 58 } 59 public String toString() { 60 return re.toString(); 61 } 62 }; 63 } catch (final REException e) { 64 throw new IllegalArgumentException (e.getMessage()) { 65 public Throwable getCause() { 66 return e; 67 } 68 }; 69 } 70 } 71 } 72 | Popular Tags |