[英]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.