Scala string replacement of entire words that comply with a pattern -


in string, how replace words start given pattern ?

for instance replace each word starts "th", "123",

val in = "this example, think of" val out = "123 123 example, 123 123 of" 

namely how replace entire words while preserving structure of sentence (consider instance comma). won't work, miss comma,

in.split("\\w+").map(w => if (w.startswith("th")) "123" else w).mkstring(" ") res: string = 123 123 example 123 123 of 

in addition punctuation marks, text may include multiple successive blanks.

you can use \bth\w* pattern words begin th followed other word characters, , replace matches "123"

scala> "this example, think of, anne hathaway".replaceall("\\bth\\w*", "123") res0: string = 123 123 example, 123 123 of, anne hathaway 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -