R:gsub保留字符串中字符之间的空格而不删除

时间:2017-09-30 02:40:49

标签: r regex gsub

我有一个字符串,它们之间带有数字字符。

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"

我想看起来像

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

到目前为止,我无法保留单词之间的空间。

gsub("[^a-z-]", "", TestString) 
#"whitetigerroars-fasthorseruns-catisuseless-dogbarks"

1 个答案:

答案 0 :(得分:1)

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"