Skip to content

Instantly share code, notes, and snippets.

@hsaputra
Last active August 29, 2015 14:08
Show Gist options
  • Save hsaputra/b78fd3a3320b88dd1b29 to your computer and use it in GitHub Desktop.
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"
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