Scala regex "starts with lowercase alphabets" not working -
val alphabetpattern = "^([a-z]+)".r def stringmatch(s: string) = s match { case alphabetpattern() => println("found") case _ => println("not found") } if try,
stringmatch("hello") i "not found", expected "found".
my understanding of regex,
[a-z] = in range of 'a' 'z'
+ = 1 more of previous pattern
^ = starts with
so regex alphabetpattern "all strings start 1 or more alphabets in range a-z"
surely missing something, want know what.
replace case alphabetpattern() case alphabetpattern(_) , works. extractor pattern takes variable binds result. here discard use x or whatever.
edit: further randall's comment below, if check docs regex you'll see has unapplyseq rather unapply method, means takes multiple variables. if have wrong number, won't match, rather
list match { case list(a,b,c) => + b + c } won't match if list doesn't have 3 elements.
Comments
Post a Comment