删除两个括号之间的所有文本 [英] Remove all text between two brackets

查看:62
本文介绍了删除两个括号之间的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些这样的文本,

Suppose I have some text like this,

text<-c("[McCain]: We need tax policies that respect the wage earners and job creators. [Obama]: It's harder to save. It's harder to retire. [McCain]: The biggest problem with American healthcare system is that it costs too much. [Obama]: We will have a healthcare system, not a disease-care system. We have the chance to solve problems that we've been talking about... [Text on screen]: Senators McCain and Obama are talking about your healthcare and financial security. We need more than talk. [Obama]: ...year after year after year after year. [Announcer]: Call and make sure their talk turns into real solutions. AARP is responsible for the content of this advertising.")

我想删除(删除) [ 和 ] 之间的所有文本(以及括号本身).做到这一点的最佳方法是什么?这是我使用正则表达式和 stingr 包的微弱尝试:

and I would like to remove (edit: get rid of) all of the text between the [ and ] (and the brackets themselves). What's the best way to do this? Here is my feeble attempt using regex and the stingr package:

str_extract(text, "\\[[a-z]*\\]")

感谢您的帮助!

推荐答案

有了这个:

gsub("\\[[^\\]]*\\]", "", subject, perl=TRUE);

正则表达式的含义:

  \[                       # '['
  [^\]]*                   # any character except: '\]' (0 or more
                           # times (matching the most amount possible))
  \]                       # ']'

这篇关于删除两个括号之间的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆