Scala capture group using regex

Posted by Geo on Stack Overflow See other posts from Stack Overflow or by Geo
Published on 2010-06-16T05:29:50Z Indexed on 2010/06/16 5:32 UTC
Read the original article Hit count: 313

Filed under:
|

Let's say I have this code:

val string = "one493two483three"
val pattern = """two(\d+)three""".r
pattern.findAllIn(string).foreach(println)

I expected findAllIn to only return 483, but instead, it returned two483three. I know I could use unapply to extract only that part, but I'd have to have a pattern for the entire string, something like:

 val pattern = """one.*two(\d+)three""".r
 val pattern(aMatch) = string
 println(aMatch) // prints 483

Is there another way of achieving this, without using the classes from java.util directly, and without using unapply?

© Stack Overflow or respective owner

Related posts about regex

Related posts about scala