1

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?

krads
  • 1,281
  • 7
  • 14
Jean
  • 65
  • 1
  • 10

1 Answers1

4

You may remove all substrings within square brackets using

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.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476