如何在 Scala 中使用正则表达式进行模式匹配?
我希望能够找到单词的第一个字母与组中的一个字母(例如“ABC”)之间的匹配项。在伪代码中,这可能看起来像:
case Process(word) =>
word.firstLetter match {
case([a-c][A-C]) =>
case _ =>
}
}
但是如何在 Scala 而不是 Java 中获取第一个字母?如何正确表达正则表达式?是否可以在 案例类 中执行此操作?
I would like to be able to find a match between the first letter of a word, and one of the letters in a group such as "ABC". In pseudocode, this might look something like:
case Process(word) =>
word.firstLetter match {
case([a-c][A-C]) =>
case _ =>
}
}
But how do I grab the first letter in Scala instead of Java? How do I express the regular expression properly? Is it possible to do this within a case class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
稍微扩展一下 Andrew 的答案:表达式定义提取器可用于使用 Scala 的模式匹配很好地分解正则表达式匹配的子字符串,例如:
To expand a little on Andrew's answer: The fact that regular expressions define extractors can be used to decompose the substrings matched by the regex very nicely using Scala's pattern matching, e.g.:
请注意,@AndrewMyers 的答案中的方法将 整个 字符串与正则表达式匹配,其效果是使用
^
和将正则表达式锚定在字符串的两端>$。示例:
末尾没有
.*
:Note that the approach from @AndrewMyers's answer matches the entire string to the regular expression, with the effect of anchoring the regular expression at both ends of the string using
^
and$
. Example:And with no
.*
at the end:String.matches 是在正则表达式意义上进行模式匹配的方法。
但顺便说一句,真正的 Scala 代码中的 word.firstLetter 看起来像:
Scala 将字符串视为 Char 的序列,因此如果出于某种原因你想显式获取字符串的第一个字符并匹配它,你可以使用类似的东西这:
我并不建议将此作为进行正则表达式模式匹配的一般方法,但它符合您建议的方法,即首先找到字符串的第一个字符,然后将其与正则表达式进行匹配。
编辑:
需要明确的是,正如其他人所说,我这样做的方式是:
只是想展示一个尽可能接近您的初始伪代码的示例。干杯!
String.matches is the way to do pattern matching in the regex sense.
But as a handy aside, word.firstLetter in real Scala code looks like:
Scala treats Strings as a sequence of Char's, so if for some reason you wanted to explicitly get the first character of the String and match it, you could use something like this:
I'm not proposing this as the general way to do regex pattern matching, but it's in line with your proposed approach to first find the first character of a String and then match it against a regex.
EDIT:
To be clear, the way I would do this is, as others have said:
Just wanted to show an example as close as possible to your initial pseudocode. Cheers!
首先我们应该知道正则表达式是可以单独使用的。这是一个例子:
其次,我们应该注意到,将正则表达式与模式匹配相结合将非常强大。这是一个简单的例子。
其实正则表达式本身就已经很强大了;我们唯一需要做的就是通过Scala 让它变得更加强大。以下是 Scala 文档中的更多示例: http://www.scala-lang.org/files/archive/api/current/index.html#scala.util.matching.Regex
First we should know that regular expression can separately be used. Here is an example:
Second we should notice that combining regular expression with pattern matching would be very powerful. Here is a simple example.
In fact, regular expression itself is already very powerful; the only thing we need to do is to make it more powerful by Scala. Here are more examples in Scala Document: http://www.scala-lang.org/files/archive/api/current/index.html#scala.util.matching.Regex
您可以这样做,因为正则表达式定义了提取器,但您需要首先定义正则表达式模式。我无法访问 Scala REPL 来测试它,但类似的东西应该可以工作。
无捕获组
仅检查 Regex< /a> 匹配,忽略任何组,
使用序列通配符:
子字符串匹配
在模式匹配中,
Regex
通常匹配整个输入。但是,未锚定的
Regex
会在任何地方找到该模式在输入中:
You can do this because regular expressions define extractors but you need to define the regex pattern first. I don't have access to a Scala REPL to test this but something like this should work.
No capturing groups
To check only whether the Regex matches, ignoring any groups,
use a sequence wildcard:
Sub-string matching
In a pattern match,
Regex
normally matches the entire input.However, an unanchored
Regex
finds the pattern anywherein the input:
从版本 2.10 开始,可以使用 Scala 的字符串插值功能:
更好的是可以绑定正则表达式组:
还可以设置更详细的绑定机制:
一个关于
Dynamic
的可能性的令人印象深刻的示例显示在博客文章动态类型简介< /em>:Since version 2.10, one can use Scala's string interpolation feature:
Even better one can bind regular expression groups:
It is also possible to set more detailed binding mechanisms:
An impressive example on what's possible with
Dynamic
is shown in the blog post Introduction to Type Dynamic:正如 delnan 指出的,Scala 中的
match
关键字与正则表达式无关。要查明字符串是否与正则表达式匹配,可以使用 String.matches 方法。要查明字符串是否以小写或大写的 a、b 或 c 开头,正则表达式将如下所示:您可以将此正则表达式读作“后面跟随的字符 a、b、c、A、B 或 C 之一”由任何内容”(
.
表示“任何字符”,*
表示“零次或多次”,因此“.*”是任何字符串)。As delnan pointed out, the
match
keyword in Scala has nothing to do with regexes. To find out whether a string matches a regex, you can use theString.matches
method. To find out whether a string starts with an a, b or c in lower or upper case, the regex would look like this:You can read this regex as "one of the characters a, b, c, A, B or C followed by anything" (
.
means "any character" and*
means "zero or more times", so ".*" is any string).