Scala 列表匹配正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14766667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala List Match Regex
提问by princess of persia
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
其中 token 是字符串列表,regex 是我想要匹配的模式
回答by
Your code should work. Are you sure your regex is correct?
您的代码应该可以工作。您确定您的正则表达式正确吗?
val regex = "a.c".r
val tokens = List("abc", "axc", "abd", "azc")
tokens filter (x => regex.pattern.matcher(x).matches)
//result: List[String] = List(abc, axc, azc)
Edit:
编辑:
Given your regex, make sure that the following examples match your expectation:
鉴于您的正则表达式,请确保以下示例符合您的期望:
val regex = """\b[b-df-hj-np-tv-z]*[aeiou]+[b-df-hj-np-tv-z]*\b""".r
regex.pattern.matcher("good").matches
//res3: Boolean = true
regex.pattern.matcher("no good deed").matches
//res4: Boolean = false
The matchesmethod will attempt to match the whole string.
该matches方法将尝试匹配整个字符串。
回答by 7zark7
Another option for completeness:
完整性的另一种选择:
val words = List("alpha", "bravo", "charlie", "alphie")
words.filter(_.matches("a.*"))
res0: List[java.lang.String] = List(alpha, alphie)
回答by nimda
Something that I've had trouble with when using Scala's Regex engine is that .matcheswill attempt to match the entire string, as opposed to doing a match on every possible substring.
我在使用 Scala 的 Regex 引擎时遇到的问题是它.matches会尝试匹配整个字符串,而不是对每个可能的子字符串进行匹配。
In many regex engines, the following code would evaluate to a match:
在许多正则表达式引擎中,以下代码将评估为匹配:
"alphie".match(/a/)
"alphie".match(/a/)
In Scala, using .matcheshere 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.
在 Scala 中,使用.matcheshere 会失败;它将尝试将“a”与整个字符串“alphie”进行匹配。但是,如果 Regex 是/a*/,它将起作用,因为该*字符将匹配零个或多个字符。
If adding repeating Regex symbols isn't an option, the findAllInmethod might be useful:
如果添加重复的 Regex 符号不是一种选择,则该findAllIn方法可能很有用:
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: findAllInmay match a particular string multiple times, if there are multiple matches in the string.
注意:findAllIn如果字符串中有多个匹配项,则可能会多次匹配特定字符串。
回答by korefn
Have you tried it like:
你有没有像这样尝试过:
val list = List("abc","efg","")
val p = java.util.regex.Pattern.compile(".*")
val matching = list filter { p.matcher(_).matches }

