Scala: Matching optional Regular Expression groups

Posted by Brian Heylin on Stack Overflow See other posts from Stack Overflow or by Brian Heylin
Published on 2010-03-17T10:31:56Z Indexed on 2010/03/17 10:41 UTC
Read the original article Hit count: 271

Filed under:
|
|

I'm trying to match on an option group in Scala 2.8 (beta 1) with the following code:

import scala.xml._

val StatementPattern = """([\w\.]+)\s*:\s*([+-])?(\d+)""".r

def buildProperty(input: String): Node = input match {
    case StatementPattern(name, value) => <propertyWithoutSign />
    case StatementPattern(name, sign, value) => <propertyWithSign />
}

val withSign = "property.name: +10"
val withoutSign = "property.name: 10"

buildProperty(withSign)        // <propertyWithSign></propertyWithSign>
buildProperty(withoutSign)     // <propertyWithSign></propertyWithSign>

But this is not working. What is the correct way to match optional regex groups?

© Stack Overflow or respective owner

Related posts about scala-2.8

Related posts about regex