Skip to content

Instantly share code, notes, and snippets.

@redsquirrel
Created October 14, 2008 10:21

Revisions

  1. redsquirrel created this gist Oct 14, 2008.
    34 changes: 34 additions & 0 deletions gistfile1.java
    Original 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);
    }

    }