Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to replace string with back reference using gsub in R

Tags:

regex

r

I am trying to replace some text in a character vector using regex in R where, if there is a set of letters inside a bracket, the bracket content is to erplace the whole thing. So, given the input:

tst <- c("85", "86 (TBA)", "87 (LAST)")

my desired output would be equivalent to c("85", "TBA", "LAST")

I tried gsub("\\(([[:alpha:]])\\)", "\\1", tst) but it didn't replace anything. What do I need to correct in my regular expression here?

like image 327
Ricky Avatar asked Dec 29 '25 05:12

Ricky


1 Answers

I think you want

gsub(".*\\(([[:alpha:]]+)\\)", "\\1", tst)
# [1] "85"   "TBA"  "LAST"

Your first expression was trying to match exactly one alpha character rather than one-or-more. I also added the ".*" to capture the beginning part of the string so it gets replaced as well, otherwise, it would be left untouched.

like image 171
MrFlick Avatar answered Dec 31 '25 19:12

MrFlick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!