Home > Software design >  R How to delete any string within []
R How to delete any string within []

Time:11-12

I have a question about deleting any string within [].

My text data has a pattern that always start with [author name, date] or so.

For example, "[Report by Jeongho Choi: "Korea's Alarms Its Citizens"] [Text] Of all ~~"

The two text within [] is useless, so I want to delete [Report by Jeongho Choi: "Korea's Alarms Its Citizens"] and [Text].

CodePudding user response:

This should do it:

srng <-  "[Report by Jeongho Choi: 'Koreas Alarms Its Citizens'] [Text] Of all ~~"

trimws(gsub("\\[[^][]*]", "", srng))

Output:

[1] "Of all ~~"

CodePudding user response:

A shorter option would be the following. The used regular expression is \\[ for opening brackets, . for any character * that appears zero or more times, followed by a closing brackets \\] and a space .

a <- c("[Report by Jeongho Choi:'Korea's Alarms Its Citizens'] [Text] Of all ~")
gsub("\\[.*\\] ", "", a)

Output

"Of all ~"
  • Related