Je suis en train de faire correspondre à un groupe d'options Scala 2.8 (beta 1) avec le code suivant:Scala: Matching en option des groupes d'expression réguliers
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>
Mais cela ne fonctionne pas. Quelle est la bonne façon de faire correspondre les groupes regex optionnels?
Scala utilise la méthode Matcher.group dans Regex.unapplySeq. Cela spécifie que si un groupe ne correspond pas à une partie de la séquence, la valeur null est renvoyée - –
Ce serait bien si Scala pouvait utiliser une classe Option pour un champ regex optionnel au lieu d'exiger une vérification nulle. –