Created
October 14, 2008 10:21
Revisions
-
redsquirrel created this gist
Oct 14, 2008 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ class SymbolReplacer { protected String stringToReplace; SymbolReplacer(String s) { this.stringToReplace = s; } String replace() { Pattern symbolPattern = Pattern.compile("\\$([a-zA-Z]\\w*)"); int startingPosition = 0; while (true) { Matcher symbolMatcher = symbolPattern.matcher(stringToReplace.substring(startingPosition)); if (symbolMatcher.find()) { startingPosition += replaceSymbol(symbolMatcher); } else break; } return stringToReplace; } private int replaceSymbol(Matcher symbolMatcher) { String symbolName = symbolMatcher.group(1); if (getSymbol(symbolName) != null) stringToReplace = stringToReplace.replace("$" + symbolName, translate(symbolName)); return symbolMatcher.start(1); } protected String translate(String symbolName) { return getSymbol(symbolName); } }