简体   繁体   English

在Scala中计算正则表达式匹配?

[英]Counting regex matches in Scala?

In Scala I am trying to detemine the number of types a particular match is found (and it can occur several times in the same string). 在Scala中,我试图确定找到特定匹配的类型数量(并且它可以在同一个字符串中多次出现)。 Namely, something that is of form "##/nnn-#" where # is a number 0-9 and n is a letter A-Za-z and - is a hiphen and / is a forward slash. 即,形式为“## / nnn-#”的东西,其中#是数字0-9 ,n是字母A-Za-z并且 - 是hiphen和/是正斜杠。

You mean something like this? 你的意思是这样的?

scala> val reg = "[0-9]{2}/[a-zA-Z]{3}-[0-9]".r
scala> val str = "12/abc-2 abcd 55/bar-2 foo bar"
scala> reg.findAllIn(str).length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM