regex -6ren">
gpt4 book ai didi

regex - 在 Scala 中使用带过滤器的正则表达式

转载 作者:行者123 更新时间:2023-12-02 11:25:27 26 4
gpt4 key购买 nike

下面的使用regex不匹配值:charIntIntIntIntIntInt :

val regex = "([a-zA-Z]\\d\\d\\d\\d\\d\\d)"
//> regex : String = ([a-zA-Z]\d\d\d\d\d\d)
val f = List("b111111").filter(fi => fi startsWith regex)
//> f : List[String] = List()
f是一个空列表,它应该包含 b111111
当我在 https://www.regex101.com/ 上使用这个正则表达式时然后它正确匹配字符串。

我的过滤方式有问题吗?

最佳答案

如何使用 Scala 语言 Regex 功能,例如:

val regex = """^([a-zA-Z]\d{6})""".r // enables you to drop escaping \'s
val f = List("b111111").filter { s => regex.findFirstIn(s).isDefined }

http://www.scala-lang.org/api/current/index.html#scala.util.matching.Regex更多细节

关于regex - 在 Scala 中使用带过滤器的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27942480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com