【发布时间】:2015-10-12 15:34:25
【问题描述】:
虽然Replace two dots in a string with gsub 回答了有关替换“.”等标点字符的问题,但它似乎不适用于单词边界。例如,
text100 <- "My # is 1234"
text1 <- gsub("\\b#\\b","hash",text100)
> text1
[1] "My # is 1234"
# 没有被替换。如何解决这个问题?
请注意,不应替换多个#。例如,
'##' 不应替换为 'hash' 或 'hashhash'。
# 后面或前面的任何图形字符都应该 不被替换(例如,'.#' 不应该被替换)
【问题讨论】:
-
这不会解决单词边界问题。例如: text100> text1 text1 [1] "My numberhashnumber hash is 1234" 注意number#号码不应该被替换。
-
请将多个连续哈希替换的要求添加到问题本身。
-
@stribizhev 完成。谢谢。
标签: regex r punctuation word-boundary word-break