I have a list in Scala as below.
val inputList :List[String] = List("Manager","VP", "12/09/2011","Access","10/11/2021 1:51 PM","Agent","Customer Contact date 07/23/2011", "Profile")
inputList: List[String] = List(Manager, VP, 12/09/2011, Access, 10/11/2021 1:51 PM, Agent, Customer Contact date 07/23/2011, Profile)
Now I want to ignore the list elements that only STARTS WITH a date. So when I filter out and print the list I should get it as the below.
Manager
VP
Access
Agent
Customer Contact date 07/23/2011
Profile
I tried as
scala>var finalList = List[String]()
scala> finalList = inputList.filterNot(r => r.startsWith("[0-9]{1,2}[/][0-9]{1,2}[/][0-9]{4}"))
finalList: List[String] = List(Manager, VP, 12/09/2011, Access, 10/11/2021 1:51 PM, Agent, Customer Contact date 07/23/2011, Profile)
I still see elements starting with date. I tried different regex format as below and still see its not ignoring them. Is the regex I am using has any issue? I tried scala> finalList = inputList.filterNot(r => r.startsWith("\\d{1,2}[/]\\d{1,2}[/]\\d{4}")
and still see its not eliminating list elements that begin with date. Can i get some advise...
Thanks
CodePudding user response:
Using startsWith does not take a regex. You could use findFirstIn
and then filterNot the values that are nonEmpty.
You might use
val inputList :List[String] = List("Manager","VP", "12/09/2011","Access","10/11/2021 1:51 PM","Agent","Customer Contact date 07/23/2011", "Profile")
val rgx = "^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}".r
val finalList: List[String] = inputList.filterNot(s =>
rgx.findFirstIn(s)
.nonEmpty
)
finalList
Output
res0: List[String] = List(Manager, VP, Access, Agent, Customer Contact date 07/23/2011, Profile)
Or use filter with isEmpty
val finalList: List[String] = inputList.filter(s => rgx.findFirstIn(s).isEmpty)