Achal Neupane
Achal Neupane

Reputation: 5719

How to perform pattern matching in R from reverse direction?

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"

Upvotes: 4

Views: 1509

Answers (2)

akrun
akrun

Reputation: 887691

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"

Upvotes: 2

David Arenburg
David Arenburg

Reputation: 92302

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

Upvotes: 5

Related Questions