如何在 Scala 2.13 中进行模式匹配? [英] How to pattern match in scala 2.13?

查看:58
本文介绍了如何在 Scala 2.13 中进行模式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下正则表达式,我想在 Scala 2.13 中进行模式匹配.
正则表达式:

I have the following regex, that I would like to pattern match in Scala 2.13.
The regex:

\/brokers\/ids\/\d{1,}$

以下字符串,将被验证:

The following string, that is going to be validate:

scala> ("echo dump" #| "nc localhost 32773" #| "grep brokers").!!
res2: String =
"       /brokers/ids/1
"

如何在 Scala 2.13 中执行此操作?

How can I do it in Scala 2.13?

推荐答案

Scala 2.13 引入了内插字符串模式,因此您可以避免使用正则表达式,只需执行以下操作:

Scala 2.13 introduced interpolated string patterns, so you could avoid using regex and just do:

"/brokers/ids/1" match {
  case s"/brokers/ids/$ids" => ids //returns 1
}

这篇关于如何在 Scala 2.13 中进行模式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆