Wang
Wang

Reputation: 1460

Remove the first bracket and its content in a string with R

For instance:

a = '[122][md]+'
b = '[3][md+5]x'

I want to remove the first bracket and the contents in this bracket, and get:

a = '[md]+'
b= '[md+5]x'

Upvotes: 0

Views: 42

Answers (2)

Dirk N
Dirk N

Reputation: 717

You can use regex

> gsub('\\[[0-9]+\\]', '', a)
[1] "[md]+"

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520928

Use sub with the pattern \[.*?\] and replace that with empty string.

a <- '[122][md]+'
b <- '[3][md+5]x'
sub("\\[.*?\\]", "", a)
sub("\\[.*?\\]", "", b

Demo

Upvotes: 1

Related Questions