How to check whether a String fully matches a Regex in Scala?

RegexScala

Regex Problem Overview


Assume I have a Regex pattern I want to match many Strings to.

val Digit = """\d""".r

I just want to check whether a given String fully matches the Regex. What is a good and idiomatic way to do this in Scala?

I know that I can pattern match on Regexes, but this is syntactically not very pleasing in this case, because I have no groups to extract:

scala> "5" match { case Digit() => true case _ => false }
res4: Boolean = true

Or I could fall back to the underlying Java pattern:

scala> Digit.pattern.matcher("5").matches
res6: Boolean = true

which is not elegant, either.

Is there a better solution?

Regex Solutions


Solution 1 - Regex

Answering my own question I'll use the "pimp my library pattern"

object RegexUtils {
  implicit class RichRegex(val underlying: Regex) extends AnyVal {
    def matches(s: String) = underlying.pattern.matcher(s).matches
  }
}

and use it like this

import RegexUtils._
val Digit = """\d""".r
if (Digit matches "5") println("match")
else println("no match")

unless someone comes up with a better (standard) solution.

Notes

  • I didn't pimp String to limit the scope of potential side effects.

  • unapplySeq does not read very well in that context.

Solution 2 - Regex

I don't know Scala all that well, but it looks like you can just do:

"5".matches("\\d")
References

Solution 3 - Regex

For the full match you may use unapplySeq. This method tries to match target (whole match) and returns the matches.

scala> val Digit = """\d""".r
Digit: scala.util.matching.Regex = \d

scala> Digit unapplySeq "1"
res9: Option[List[String]] = Some(List())

scala> Digit unapplySeq "123"
res10: Option[List[String]] = None

scala> Digit unapplySeq "string"
res11: Option[List[String]] = None

Solution 4 - Regex

  """\d""".r.unapplySeq("5").isDefined            //> res1: Boolean = true
  """\d""".r.unapplySeq("a").isDefined            //> res2: Boolean = false

Solution 5 - Regex

The answer is in the regex:

val Digit = """^\d$""".r

Then use the one of the existing methods.

Solution 6 - Regex

Using Standard Scala library and a pre-compiled regex pattern and pattern matching (which is scala state of the art):

val digit = """(\d)""".r

"2" match {
  case digit( a) => println(a + " is Digit")
  case _ => println("it is something else")
}

more to read: http://www.scala-lang.org/api/2.12.1/scala/util/matching/index.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmkneisslView Question on Stackoverflow
Solution 1 - RegexmkneisslView Answer on Stackoverflow
Solution 2 - RegexpolygenelubricantsView Answer on Stackoverflow
Solution 3 - RegexVasil RemeniukView Answer on Stackoverflow
Solution 4 - RegexJackView Answer on Stackoverflow
Solution 5 - RegexDaniel C. SobralView Answer on Stackoverflow
Solution 6 - RegexSvenView Answer on Stackoverflow