Regex defines unapplySeq , not unapply , which means that you get each group in its variable. In addition, although in some cases younger matches may work, i.e. With options, you really should use uppercase. So what will work:
val Pat1 = """ab""".r val Pat2 = """(a)(b)""".r val Pat3 = """((a)(b))""".r val Pat4 = """((a)b)""".r val Pat5 = """(ab)""".r def no() { println("No match") } "ab" match { case Pat1() => println("Pat1"); case _ => no } "ab" match { case Pat2(x,y) => println("Pat2 "+x+" "+y); case _ => no } "ab" match { case Pat3(x,y,z) => println("Pat3 "+x+" "+y+" "+z); case _ => no } "ab" match { case Pat4(x,y) => println("Pat4 "+x+" "+y); case _ => no } "ab" match { case Pat5(x) => println("Pat5 "+x); case _ => no }
(You will always get a match.)
If you want all matches, use @ _*
"ab" match { case Pat3(w @ _*) => println(w); case _ => no }
I'm not sure what you mean by (?a) , so I donโt know what is wrong with him. Do not confuse (?a) with (?:a) (or with (a?) Or with (a)? ).
source share