Scala regex examples

Scala has a neat built-in function that turns a string into a regular expression (.r). If you use it on a regular expression with slashes in it, you’ll get errors like so:

"\w+".r
:1: error: invalid escape character
       "\w+".r
         ^

Thus for many regexes, it is preferable to use the multi-line string syntax, as this skips the escaping.

scala> """\w+""".r
res43: scala.util.matching.Regex = \w+

From this, we can do some neat things, like find the first word in a sentence:

x.findFirstMatchIn("abc def")
res44: Option[scala.util.matching.Regex.Match] = Some(abc)

We can also replace all matches in the string, so if we want to swap one word for another, we can:

x.replaceAllIn("abc def", "gary")
res46: String = gary gary

Leave a Reply

Your email address will not be published. Required fields are marked *