Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regex with filter in Scala

Tags:

regex

scala

The use of below regex does not match value : charIntIntIntIntIntInt :

val regex = "([a-zA-Z]\\d\\d\\d\\d\\d\\d)"
       //> regex  : String = ([a-zA-Z]\d\d\d\d\d\d)
val f = List("b111111").filter(fi => fi startsWith regex)
       //> f  : List[String] = List()

f is an empty List, it should contain b111111

When I use this regex on https://www.regex101.com/ then it correctly matches the String.

Is there a problem with how I'm filtering ?

like image 873
blue-sky Avatar asked Jan 14 '15 12:01

blue-sky


1 Answers

Need to use matches instead of startsWith

This is detailed in String.class

This works :

val regex = "([a-zA-Z]\\d\\d\\d\\d\\d\\d)" 
val f = List("b111111").filter(fi => fi matches regex)
like image 187
blue-sky Avatar answered Sep 27 '22 21:09

blue-sky