Scala string replacement of entire words that comply with a pattern

2.6k views Asked by At

In a string, how to replace words that start with a given pattern ?

For instance replace each word that starts with "th", with "123",

val in = "this is the example, that we think of"
val out = "123 is 123 example, 123 we 123 of"

Namely how to replace entire words while preserving the structure of the sentence (consider for instance the comma). This won't work, we miss the comma,

in.split("\\W+").map(w => if (w.startsWith("th")) "123" else w).mkString(" ")
res: String = 123 is 123 example 123 we 123 of

In addition to punctuation marks, the text may include multiple successive blanks.

2

There are 2 answers

1
dcastro On BEST ANSWER

You can use the \bth\w* pattern to look for words that begin with th followed by other word characters, and then replace all matches with "123"

scala> "this is the example, that we think of, anne hathaway".replaceAll("\\bth\\w*", "123")
res0: String = 123 is 123 example, 123 we 123 of, anne hathaway
2
Identity1 On

The above regex in "replaceALL()" might fail if we add "while bathing" in the line, it requires a word boundary as below.

  val in = "this is the example, that we think of while bathing"
  out = in.replaceAll("\\bth\\w*", "123")
  out: String = 123 is 123 example, 123 we 123 of while bathing