| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.regex.Matcher; |
| import java.util.regex.Pattern; |
| |
| public class Regexp { |
| private Map mapReplacers = new HashMap(); |
| private String msgLastRunReplaceAll; |
| private Pattern p = null; |
| |
| public void init(String inStrMapReplacers, String pattern) { |
| p = Pattern.compile(pattern); |
| String [] replacerPairs = split(inStrMapReplacers, ":"); |
| for (int i=0; i < replacerPairs.length; i++) { |
| String[] replacerTokens = split(replacerPairs[i], "~"); |
| if (replacerTokens.length != 2) { |
| throw new RuntimeException("The initializer String for mapReplacers is wrong. Should have 2 tokens separated by '~'. Position: " + replacerPairs[i]); |
| } |
| mapReplacers.put(replacerTokens[0], replacerTokens[1]); |
| } |
| } |
| |
| |
| |
| public Map getMapReplacers () { |
| return mapReplacers; |
| } |
| |
| public String getMsgLastRunReplaceAll() { |
| return msgLastRunReplaceAll; |
| } |
| |
| |
| public int count(String line) { |
| int i = 0; |
| |
| Matcher m = p.matcher(line); |
| boolean result = m.find(); |
| while (result) { |
| i++; |
| result = m.find(); |
| } |
| |
| return i; |
| } |
| |
| public String replaceAll(String line) { |
| msgLastRunReplaceAll = ""; |
| |
| Matcher m = p.matcher(line); |
| StringBuffer sb = new StringBuffer(); |
| boolean result = m.find(); |
| while (result) { |
| String toReplace = m.group(); |
| String replacer = (String) mapReplacers.get(toReplace); |
| if (replacer == null) { |
| String msg = "Cannot find replacer for " + toReplace + "\n"; |
| msgLastRunReplaceAll = msgLastRunReplaceAll.length() == 0 ? msg : msgLastRunReplaceAll + "\n" + msg; |
| |
| |
| m.appendReplacement(sb,toReplace); |
| } else { |
| m.appendReplacement(sb, replacer); |
| } |
| |
| result = m.find(); |
| } |
| m.appendTail(sb); |
| return sb.toString(); |
| } |
| |
| |
| public static void main (String args[]) { |
| |
| Regexp cut = new Regexp(); |
| String unformattedText = "\nLieber <<supporter>>,\n\n" + |
| "du hast heute <<Datum>> mal wieder vergessen, das Ticket ( <<reqNumBer>> ) zu bearbeiten!!\n\n" + |
| "Pöser Pursche, du. :-)"; |
| String pattern= "<{2}(.[aA-zZ]+?)>{2}"; |
| |
| System.out.println("**test count()**"); |
| cut.init("", pattern); |
| int res = cut.count(unformattedText); |
| System.out.println("result =" + res); |
| |
| System.out.println("**testReplaceAllSuccess()**"); |
| cut = new Regexp(); |
| cut.init("<<supporter>>~Herr Supporter:<<Datum>>~19.03.1994:<<reqNumBer>>~42", pattern); |
| String resStr = cut.replaceAll(unformattedText); |
| System.out.println("result =" + resStr); |
| System.out.println("for problems in replacement::" + cut.getMsgLastRunReplaceAll()); |
| |
| System.out.println("**testReplaceWithUnknownToReplaceToken()**"); |
| cut = new Regexp(); |
| cut.init("<<supporter>>~Herr Supporter:<<Datum>>~19.03.1994:<<reqNumBer>>~42", pattern); |
| resStr = cut.replaceAll("<<thatDoesNotExist>>" + unformattedText); |
| |
| System.out.println("result=" + resStr); |
| System.out.println("for problems in replacement::" + cut.getMsgLastRunReplaceAll()); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static String[] split(String str, String separatorChars) { |
| return splitWorker(str, separatorChars, -1, false); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) { |
| |
| |
| |
| |
| if (str == null) { |
| return null; |
| } |
| int len = str.length(); |
| if (len == 0) { |
| |
| |
| return new String[0]; |
| } |
| List list = new ArrayList(); |
| int sizePlus1 = 1; |
| int i = 0, start = 0; |
| boolean match = false; |
| boolean lastMatch = false; |
| if (separatorChars == null) { |
| |
| while (i < len) { |
| if (Character.isWhitespace(str.charAt(i))) { |
| if (match || preserveAllTokens) { |
| lastMatch = true; |
| if (sizePlus1++ == max) { |
| i = len; |
| lastMatch = false; |
| } |
| list.add(str.substring(start, i)); |
| match = false; |
| } |
| start = ++i; |
| continue; |
| } else { |
| lastMatch = false; |
| } |
| match = true; |
| i++; |
| } |
| } else if (separatorChars.length() == 1) { |
| |
| char sep = separatorChars.charAt(0); |
| while (i < len) { |
| if (str.charAt(i) == sep) { |
| if (match || preserveAllTokens) { |
| lastMatch = true; |
| if (sizePlus1++ == max) { |
| i = len; |
| lastMatch = false; |
| } |
| list.add(str.substring(start, i)); |
| match = false; |
| } |
| start = ++i; |
| continue; |
| } else { |
| lastMatch = false; |
| } |
| match = true; |
| i++; |
| } |
| } else { |
| |
| while (i < len) { |
| if (separatorChars.indexOf(str.charAt(i)) >= 0) { |
| if (match || preserveAllTokens) { |
| lastMatch = true; |
| if (sizePlus1++ == max) { |
| i = len; |
| lastMatch = false; |
| } |
| list.add(str.substring(start, i)); |
| match = false; |
| } |
| start = ++i; |
| continue; |
| } else { |
| lastMatch = false; |
| } |
| match = true; |
| i++; |
| } |
| } |
| if (match || (preserveAllTokens && lastMatch)) { |
| list.add(str.substring(start, i)); |
| } |
| return (String[]) list.toArray(new String[list.size()]); |
| } |
| } |