How to replace regular expression patterns in strings in Scala
Scala String FAQ: How do I replace a regular expression (regex) pattern in a String
in Scala?
Solution
Because a String
is immutable, you can’t perform find-and-replace operations directly on it, but you can create a new String
that contains the replaced contents. There are several ways to do this.
You can call replaceAll
on a String
, remembering to assign the result to a new variable: