Improving Scala's Regex Support

Scala currently has a method StringLike.r for compiling strings to regexes.

Now that Scala has string interpolation and macros (since 2.10) I think we can do even better with a r method on StringContext. Here is why:

  1. Regexes can be syntax-checked at compile-time
  2. Escaping gets simpler
    Before: "\\d+".r
    Before (alt.): """\d+""".r
    After: r"\d+"
  3. Parameterization gets simpler
    Before: ("foo"+Pattern.escape(bar)).r
    After: r"foo$bar"
  4. Pattern matching against regexes gets simpler
    Before: val re = """\d+(.*)\d+""".r; s match { case `re`(x) => x }
    After: case r"\d+$x\d+" => x

I have a proof of concept implementation available.

Note: This is filed in the Scala issue tracker as SI-7496.