2012-05-08 110 views
1

我有以下簡單的應用程序:爲什麼這個scala正則表達式匹配失敗?

object TestPatternMatch extends App { 

    if (args.length != 1) 
    throw new IllegalArgumentException("takes one argument which is a regex string that will be used to limit the org deletion") 

    val pattern = args(0).r 
    println("looking for orgs with name matching regex: " + pattern) 

    val orgs = Seq("zephyr-test-123", "abcdef", "zephyr-test-xyz-xyz-xyz") 

    orgs.foreach { 
    _ match { 
     case pattern(x) ⇒ println("matched " + x) 
     case y   ⇒ println("failed to match " + y) 
    } 
    } 
} 

當我這樣稱呼它下面,我期待相匹配,對第一和第三機構單位。我錯過了什麼?

[info] Running TestPatternMatch zephyr-test-.* 
looking for orgs with name matching regex: zephyr-test-.* 
failed to match zephyr-test-123 
failed to match abcdef 
failed to match zephyr-test-xyz-xyz-xyz 

回答

8

你的模式不包含匹配組x所需的()

val R0 = "zephyr-test-.*".r 
val R0() = "zephyr-test-123" // matches, with no assignments 
val R0(x) = "zephyr-test-123" // MatchError, nothing to bind `x` to 

val R1 = "zephyr-test-(.*)".r 
val R1(x) = "zephyr-test-123" // matches, x = "123" 
+0

doh!謝謝。將接受計時器讓我... – jxstanford

相關問題