删除 r 中方括号内的任何文本 [英] Remove any text inside square brackets in r

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

问题描述

我想删除方括号内的所有单词以及括号本身.例如,

I would like to remove all the words inside square brackets as well as the brackets themselves. For example,

text = c('[Verse 1]', '[Verse 1: Dua Lipa]', '[Corus]', '[Corus: Ann Marie & Ed Sheeran]')

像上面一样,括号内的单词长度不是恒定的.所以我需要一个可以识别 [] 位置的函数,以便擦除它们之间的所有单词、数字和符号.有什么功能可以做到这一点吗?

Like above, the length of words inside the bracket are not constant. So I need a function that can identify the position of [ and ] in order to erase all the words, numbers and symbols in between. Is there any function able to do that?

推荐答案

您可以使用

gsub("\\[[^][]*]", "", text)

该模式匹配一​​个左方括号,然后是除方括号之外的任何零个或多个字符,然后是一个右方括号.

The pattern matches an open square bracket, then any zero or more chars other than square brackets, and then a close square bracket.

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

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