如何从Scala中的文本中删除数字?

时间:2015-05-27 05:44:37

标签: regex scala text apache-spark

如何在Scala中删除数字?

例如我有这样的文字:

canon 40 22mm lens lock strength plenty orientation 321 .
删除后

canon lens lock strength plenty orientation .

3 个答案:

答案 0 :(得分:7)

请尝试filterfilterNot

val text = "canon 40 22mm lens lock strength plenty orientation 321 ."
val without_digits = text.filter(!_.isDigit)

val text = "canon 40 22mm lens lock strength plenty orientation 321 ."
val without_digits = text.filterNot(_.isDigit)

答案 1 :(得分:1)

\\d+\\S*\\s+

试试这个。empty string。见。演示。

https://regex101.com/r/tS1hW2/1

答案 2 :(得分:1)

因为很明显,你想要删除包含数字的所有单词,因为在你的例子中mm也消失了,因为它以数字作为前缀。

val s = "That's 22m, which  is gr8."
s.split(" ").filterNot(_.exists(_.isDigit)).mkString(" ")

res8: String = That's which  is