简体   繁体   中英

R: gsub keep the space between characters in a string without removing

I have a string that with numeric characters between them.

TestString = "white tiger roars.12.03.001-fast horse runs13.15.01.001-cat is useless 11.01.09.001-dog barks22.07.01.001"

I wan't it to look like

"white tiger roars-fast horse runs-cat is useless-dog barks"

With my approach till now, I am not able to retain the space between the words.

gsub("[^a-z-]", "", TestString) 
#"whitetigerroars-fasthorseruns-catisuseless-dogbarks"
gsub("\\s?(\\d+|\\.)","","white tiger roars.12.03.001-fast horse runs13.15.01.001-cat is useless 11.01.09.001-dog barks22.07.01.001")
[1] "white tiger roars-fast horse runs-cat is useless-dog barks"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM