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:
"\\d+".r
"""\d+""".r
r"\d+"
("foo"+Pattern.escape(bar)).r
r"foo$bar"
val re = """\d+(.*)\d+""".r; s match { case `re`(x) => x }
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.