How to substitute a special character between words in R [duplicate]

假如想象 提交于 2019-11-29 16:17:54

Try

gsub('(\\w)\\.(\\w)', '\\1 \\2', str)
#[1] ".wow"            "if."             "not confident"   "wonder"         
#[5] "have difficulty" "shower"       

Or

gsub('(?<=[^.])[.](?=[^.])', ' ', str, perl=TRUE)

Or as @rawr suggested

gsub('\\b\\.\\b', ' ', str, perl = TRUE)

Using capturing groups and back-references:

sub('(\\w)\\.(\\w)', '\\1 \\2', str)
# [1] ".wow"            "if."             "not confident"   "wonder"         
# [5] "have difficulty" "shower"

A capturing group can be created by placing the characters to be grouped inside a set of parenthesis ( ... ). Backreferences recall what was matched by a capturing group.

A backreference is specified as (\); followed by a digit indicating the number of the group.

Using lookaround assertions:

Lookarounds are zero-width assertions. They don't "consume" any characters on the string.

sub('(?<=\\w)\\.(?=\\w)', ' ', str, perl = TRUE)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!