模式匹配和替换-一个或多个相同模式/字符

时间:2019-09-05 11:21:41

标签: r replace pattern-matching

我想用空格替换字符串中的一个或多个句点。

我已经尝试了以下方法,但是要么没有句号被替换为空白,要么整个字符串都被替换为空白。

# Toy Data
book = c("eggs.", "flour...", "..butter", "bakingpowder", "coco..dark")

# Attempt at replacing full stops with blanks
gsub(".+", "", book)

sub("(.+)", "", book)

gsub(".\\+", "", book)

sub("(.\\+)", "", book)

# Expected result
book = c("eggs", "flour", "butter", "bakingpowder", "cocodark")

感谢您的帮助!

0 个答案:

没有答案