繁体   English   中英

R:使用gsub()替换文本中的特殊字符

[英]R: Replacing special characters in a text with gsub()

我想在文本中替换一系列表情符号。 我尝试了以下方法:

gsub(":)", "xxx", "some text >:) more text :) even more text", fixed = TRUE)
  • 返回值: some text >xxx more text xxx even more text

gsub("\\b:)\\b", "xxx", "some text >:) more text :) even more text")
  • 这将返回: some text >:) more text :) even more text

也就是说,我想替换:)而不是>:) 使用\\\\b可以解决文本问题,但是这次不构成构成笑脸的空格字符。

是的,我可以使用" :) " ,但是对我来说,这并不是最优雅的解决方案。 我有哪些选择可以更好地控制更换过程?

请尝试以下操作:

gsub("(^|(?<=\\s)):\\)", "xxx", ":) some text >:) more text :) even more text", perl=TRUE)
## [1] "xxx some text >:) more text xxx even more text"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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