Skip to content

Instantly share code, notes, and snippets.

@dohzya
Last active October 9, 2015 05:47
Show Gist options
  • Save dohzya/3448195 to your computer and use it in GitHub Desktop.
Save dohzya/3448195 to your computer and use it in GitHub Desktop.
Make regexp simple to use
import scala.util.matching.Regex
object RubyRegex {
case class RubyMatcher(rx: Regex) extends AnyVal { // Value Class (no runtime overhead!)
def ===(str: String) = rx.findFirstIn(str).isDefined
}
implicit def Regex2RubyMatcher(rx: Regex) = RubyMatcher(rx)
case class RubyMatched(str: String) extends AnyVal { // Value Class (no runtime overhead!)
def =~(rx: Regex) = rx.findFirstIn(str).isDefined
}
implicit def String2RubyMatched(str: String) = RubyMatched(str)
}
// import RubyRegex._
// println("coucou" =~ "^c".r) // => true
// println("coucou" =~ "^ac".r) // => false
// println("""^\d+$""".r === "123") // => true
// println("""^\d+$""".r === "12a3") // => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment