How do I remove numbers from text in Scala?

How do I remove numbers from text in Scala?

for example I have this text:

canon 40 22mm lens lock strength plenty orientation 321 .

      

after deletion:

canon lens lock strength plenty orientation .

      

+3


source to share


3 answers


Please try filter

orfilterNot

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

      



or

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

      

+6


source


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

      

Try it. Replace empty string

. View a demo.



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

+1


source


Since it is obvious that you want to remove all words containing a number, because it is mm

not used in your example either as it is prefixed with a number.

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

res8: String = That which  is

      

+1


source







All Articles