Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform pattern matching in R from reverse direction?

Tags:

regex

r

I have an object called mystring. I want to remove the character by matching the first delimiter "_" from backward direction and get the result.

mystring<- c("apple_tar", "banana_bag_cool", "mango_mellow_yellow_sweet")

result

"apple"  "banana_bag"   "mango_mellow_yellow"
like image 611
MAPK Avatar asked Oct 28 '25 09:10

MAPK


2 Answers

A similar approach is to separate each string into two groups and just exctract the first one

sub("(.*)(_.*)", "\\1", mystring)
## [1] "apple"               "banana_bag"          "mango_mellow_yellow"

.* will match anything and put it into the first group until it will encounter the last _ and put everything on into the second group. This approach gives you a bit more control because you can select either group at your will by specifying \\1 or \\2

like image 156
David Arenburg Avatar answered Oct 29 '25 22:10

David Arenburg


We can use sub to match an underscore(_) followed by one or more characters upto the end of the string ($) that is not an underscore ([^_]), and replace it by ''.

 sub('_[^_]+$', '', mystring)
 #[1] "apple"               "banana_bag"          "mango_mellow_yellow"

Or another option is str_extract, where we extract the characters succeeded by underscore followed by one or more characters that is not an underscore until the end of the string. Here, we use the lookaround ((?=_[^_]+$)).

 library(stringr)
 str_extract(mystring, '.*(?=_[^_]+$)')
 #[1] "apple"               "banana_bag"          "mango_mellow_yellow"
like image 22
akrun Avatar answered Oct 29 '25 23:10

akrun



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!