I was trying to replace fourth occurrence of '_' in a string. For example,
Input
AAA_BBB_CCC_DD_D_EEE
Output
AAA_BBB_CCC_DDD_EEE
Can anyone please suggest a solution?
You could use a back-reference....
gsub( "(_[^_]+_[^_]+_[^_]+)_" , "\\1" , x )
# [1] "AAA_BBB_CCC_DDD_EEE"
EDIT And thanks to @SonyGeorge below this could be further simplified to:
gsub( "((_[^_]+){3})_" , "\\1" , x )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With