7

可能重复:
删除括号、括号和/或大括号内的文本

我想替换大文本文件中的括号和括号之间的文本。

示例输入(文本文件中的内容):

Keep me (Remove Me 1). Again keep me (Remove Me 2). Again again keep me (Remove Me 3).

输出(新文本文件中的内容):

Keep me. Again keep me. Again again keep me. 

是否可以在 R 中执行此操作(例如使用 grep)?

4

1 回答 1

27

是的,用gsub()空字符串替换所有不需要的文本。

x <- "Keep me (Remove Me 1). Again keep me (Remove Me 2). Again again keep me (Remove Me 3)."

这是您想要的正则表达式:

gsub( " *\\(.*?\\) *", "", x)
[1] "Keep me. Again keep me. Again again keep me."

它是这样工作的:

  • *?在括号之前(和之后)找到 0 个或多个空格。
  • 由于()是正则表达式中的特殊符号,因此您需要转义这些符号,即 (\\(
  • The .*? is a wildcard find to find all characters, where the ? means to find in a non-greedy way. This is necessary because regex is greedy by default. In other words, by default the regex will start the match at the first opening parentheses and ends the match at the last closing parentheses.
于 2012-11-23T12:54:14.577 回答