나는 다음과 같은 코드 스칼라 2.8 (베타 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>
하지만이 작동하지 않습니다. 선택적 정규식 그룹을 일치시키는 올바른 방법은 무엇입니까?
스칼라는 Regex.unapplySeq에서 Matcher.group 메소드를 사용합니다. 그룹이 순서의 일부와 일치하지 않으면 null이 반환되도록 지정합니다. –
스칼라가 null 체크를 필요로하지 않고 선택적 정규식 필드에 Option 클래스를 사용할 수 있다면 좋을 것입니다. –