我正在尝试使用以下代码在 Scala 2.8(beta 1)中匹配选项组:
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>
但是这样不起作用。匹配可选的正则表达式组的正确方法是什么?
null
的语句应该首先列出,否则sign
变量将包含null
。” - dr jerry