gpt4 book ai didi

regex - 删除两个括号之间的所有文本

转载 作者:行者123 更新时间:2023-12-03 15:13:20 25 4
gpt4 key购买 nike

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

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 包的微弱尝试:
str_extract(text, "\\[[a-z]*\\]")

谢谢你的帮助!

最佳答案

有了这个:

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

正则表达式的含义:
  \[                       # '['
[^\]]* # any character except: '\]' (0 or more
# times (matching the most amount possible))
\] # ']'

关于regex - 删除两个括号之间的所有文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com