diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-02-09 15:31:11 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-02-09 15:31:11 +0000 |
commit | 71e03e4aca90be26290ab222d2d5131c5b2d42db (patch) | |
tree | fd1da647346c01e7b0b447637d1502c74844a22b | |
parent | 1b99b4b148b04d0eafd6bd3fb55309c29f60eb2d (diff) | |
download | scala-71e03e4aca90be26290ab222d2d5131c5b2d42db.tar.gz scala-71e03e4aca90be26290ab222d2d5131c5b2d42db.tar.bz2 scala-71e03e4aca90be26290ab222d2d5131c5b2d42db.zip |
`replaceSomeIn` method added.
-rw-r--r-- | src/library/scala/util/matching/Regex.scala | 22 | ||||
-rw-r--r-- | test/files/run/ReplacementMatching.scala | 11 |
2 files changed, 14 insertions, 19 deletions
diff --git a/src/library/scala/util/matching/Regex.scala b/src/library/scala/util/matching/Regex.scala index 6f0e4d2a16..9e101eb0b8 100644 --- a/src/library/scala/util/matching/Regex.scala +++ b/src/library/scala/util/matching/Regex.scala @@ -111,30 +111,24 @@ class Regex(regex: String, groupNames: String*) { * Replaces all matches using a replacer function. * * @param target The string to match. - * @param replacer The function which maps a matched string to another string. + * @param replacer The function which maps a match to another string. * @return The target string after replacements. */ - def replaceAllIn(target: java.lang.CharSequence, replacer: String => String): String = { - val it = new Regex.MatchIterator(target, this, groupNames) with Replacement + def replaceAllIn(target: java.lang.CharSequence, replacer: Match => String): String = { + val it = new Regex.MatchIterator(target, this, groupNames).replacementData while (it.hasNext) { - val matchedString = it.next - it.replace(replacer(matchedString)) + val matchdata = it.next + it.replace(replacer(matchdata)) } it.replaced } - /** - * Replaces all matches using a replacer function. - * - * @param target The string to match. - * @param replacer The function which maps a match to another string. - * @return The target string after replacements. - */ - def replaceAllMatchesIn(target: java.lang.CharSequence, replacer: Match => String): String = { + def replaceSomeIn(target: java.lang.CharSequence, replacer: Match => Option[String]): String = { val it = new Regex.MatchIterator(target, this, groupNames).replacementData while (it.hasNext) { val matchdata = it.next - it.replace(replacer(matchdata)) + val replaceopt = replacer(matchdata) + if (replaceopt != None) it.replace(replaceopt.get) } it.replaced } diff --git a/test/files/run/ReplacementMatching.scala b/test/files/run/ReplacementMatching.scala index 30c525b63c..05040d98a3 100644 --- a/test/files/run/ReplacementMatching.scala +++ b/test/files/run/ReplacementMatching.scala @@ -15,18 +15,19 @@ object Test { def replacementMatching { val regex = """\$\{(.+?)\}""".r - val replaced = regex.replaceAllMatchesIn("Replacing: ${main}. And another method: ${foo}.", + val replaced = regex.replaceAllIn("Replacing: ${main}. And another method: ${foo}.", (m: util.matching.Regex.Match) => { val identifier = m.group(1) identifier }) assert(replaced == "Replacing: main. And another method: foo.") - val regex2 = """\$\{(.+?)\}""".r - val replaced2 = regex2.replaceAllIn("Replacing: ${main}. And then one more: ${bar}.", (s: String) => { - "$1" + val regex3 = """\$\{(.+?)\}""".r + val replaced3 = regex3.replaceSomeIn("Replacing: ${main}. And another: ${foo}.", (m: util.matching.Regex.Match) => { + val id = m.group(1) + if (id.startsWith("m")) Some(id) else None }) - assert(replaced2 == "Replacing: main. And then one more: bar.") + assert(replaced3 == "Replacing: main. And another: ${foo}.") } def groupsMatching { |