Rozita
Rozita

Reputation: 289

How to remove numbers from text in Scala?

How to remove numbers form a text in Scala ?

for example i have this text:

canon 40 22mm lens lock strength plenty orientation 321 .

after removing :

canon lens lock strength plenty orientation .

Upvotes: 5

Views: 5481

Answers (3)

Rok Kralj
Rok Kralj

Reputation: 48745

Since it is apparent, you want to remove all words that contain a number, because in your example mm is also gone, because it is prefixed by a number.

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

res8: String = That's which  is

Upvotes: 1

user3278460
user3278460

Reputation:

Please, try filter or filterNot

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)

Upvotes: 8

vks
vks

Reputation: 67968

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

Try this.Replace by empty string.See demo.

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

Upvotes: 2

Related Questions