Scala List Match Regex

11,725

Solution 1

Another option for completeness:

val words = List("alpha", "bravo", "charlie", "alphie")
words.filter(_.matches("a.*"))

res0: List[java.lang.String] = List(alpha, alphie)

Solution 2

Have you tried it like:

val list = List("abc","efg","")
val p = java.util.regex.Pattern.compile(".*")

val matching = list filter { p.matcher(_).matches }

Solution 3

Something that I've had trouble with when using Scala's Regex engine is that .matches will attempt to match the entire string, as opposed to doing a match on every possible substring.

In many regex engines, the following code would evaluate to a match:

"alphie".match(/a/)

In Scala, using .matches here would fail; it will attempt to match "a" against the entire string "alphie". However, if the Regex was /a*/, it would work, since the * character will match zero or many characters.

If adding repeating Regex symbols isn't an option, the findAllIn method might be useful:

val words = List("alpha", "bravo", "charlie", "alphie")

val regex = "a.".r                                

//returns a tuple with the list item that matched, plus the text that fit the regex
for {
    word <- words
    matches <- regex.findAllIn(word)
} yield (word,matches)

Note: findAllIn may match a particular string multiple times, if there are multiple matches in the string.

Share:
11,725
princess of persia
Author by

princess of persia

Python, MATLAB, Java, C, Data Structure and Algorithms

Updated on June 04, 2022

Comments

  • princess of persia
    princess of persia almost 2 years

    I have a list of strings and a regex pattern. I would like to filter the the items from the list that don't match the regex. I am using the following code which doesn't seem to work:

    val matching = token.filter(x => regex.pattern.matcher(x).matches)
    

    where token is the list of strings and regex is the pattern I want to match

  • Jean-Philippe Pellet
    Jean-Philippe Pellet over 11 years
    This reparses the regular expression for each element in the list.
  • Randall Schulz
    Randall Schulz over 11 years
    Yes. Match means match! Not search or find.
  • Ken Williams
    Ken Williams over 9 years
    I think @nimda is aware of that, but it's surprising that a conceptually simple concept like regex.matchesSomewhere(_) doesn't exist in the current API. @nimda, one option is to use the .unanchored method.