Last active
August 29, 2015 14:08
-
-
Save hsaputra/b78fd3a3320b88dd1b29 to your computer and use it in GitHub Desktop.
Find words in patter of ${keyword}. So in "This is a small ${world}" the function will find and replace ${word} with "foundone"
This file contains hidden or 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 characters
def lookup(base: String, found: Map[String, String]): String = { | |
var resolved = base | |
val pattern = Pattern.compile("(\\$\\{([^{}]*)\\})") | |
val matcher = pattern.matcher(base) | |
while (matcher.find()) { | |
val a = matcher.group(2) | |
if (!found.containsKey(a)) { | |
val b = lookup("foundone", found) | |
found.put(a, b) | |
print("Found " + b) | |
resolved = resolved.replaceFirst("(\\$\\{([^\\${}]*)\\})", b) | |
} else { | |
val b = found.get(a); | |
print("Found " + b) | |
resolved = resolved.replaceFirst("(\\$\\{([^\\${}]*)\\})", b) | |
} | |
} | |
resolved | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment