user51819
user51819

Reputation: 323

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). 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.

Upvotes: 6

Views: 2966

Answers (1)

hwnd
hwnd

Reputation: 70732

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

Upvotes: 7

Related Questions