首页 > 解决方案 > 如何删除R中括号内的标点符号

问题描述

我曾尝试将文档拆分为句子,但由于括号内的标点符号,会出现一些奇怪的结果。所以我想删除任何标点符号。

示例输入:

A <- c('How to remove all punctuations(like this?) in side it?')

想要的输出:

"How to remove all punctuations(like this) in side it?"

标签: rbracketspunctuation

解决方案


也许像这样使用积极的前瞻?

gsub("[?!;,.](?=\\))", "", A, perl = T)
#[1] "How to remove all punctuations(like this) in side it?"

或使用 POSIX 字符类

gsub("[[:punct:]](?=\\))", "", A, perl = T)

或者如果您需要匹配其他类型的右括号(例如花括号、方括号)

gsub("[[:punct:]](?=[)\\]}])", "", A, perl = T)

推荐阅读