如何检查字符串是否与Scala中的正则表达式完全匹配?

时间:2021-09-02 00:08:20

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

假设我有一个Regex模式,我想要匹配许多字符串。

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?

我只是想检查给定的字符串是否完全匹配Regex。在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:

我知道我可以在Regexes上进行模式匹配,但是在这种情况下,语法上不太好理解,因为我没有组可以提取:

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

Or I could fall back to the underlying Java pattern:

或者我可以回到底层Java模式:

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

which is not elegant, either.

这也不优雅。

Is there a better solution?

有更好的解决方案吗?

6 个解决方案

#1


51  

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.

    《unapplySeq》在这种情况下读得不太好。

#2


46  

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

我不太了解Scala,但看起来你可以做到:

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

References

#3


13  

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

对于完整的匹配,您可以使用unapplySeq。此方法尝试匹配目标(整个匹配)并返回匹配。

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

#4


9  

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

#5


3  

The answer is in the regex:

答案在regex中:

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

Then use the one of the existing methods.

然后使用现有的方法之一。

#6


-1  

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

使用标准Scala库和预编译的regex模式和模式匹配(这是Scala的最新技术):

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

更多阅读:http://www.scala-lang.org/api/2.12.1/scala/util/matching/index.html

#1


51  

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.

    《unapplySeq》在这种情况下读得不太好。

#2


46  

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

我不太了解Scala,但看起来你可以做到:

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

References

#3


13  

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

对于完整的匹配,您可以使用unapplySeq。此方法尝试匹配目标(整个匹配)并返回匹配。

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

#4


9  

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

#5


3  

The answer is in the regex:

答案在regex中:

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

Then use the one of the existing methods.

然后使用现有的方法之一。

#6


-1  

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

使用标准Scala库和预编译的regex模式和模式匹配(这是Scala的最新技术):

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

更多阅读:http://www.scala-lang.org/api/2.12.1/scala/util/matching/index.html