1

I would like to get the last substring of a variable (the last part after the underscore), in this case: "myvar".

x = "string__subvar1__subvar2__subvar3__myvar"

my attempts result in a match starting from the first substring, e.g.

library(stringr)
str_extract(x, "__.*?$)

How do I do this in R?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Henk
  • 3,634
  • 5
  • 28
  • 54

3 Answers3

6

You can do

sub('.*\\__', '', x)
user2510479
  • 1,528
  • 13
  • 17
3

You can do:

library(stringr)
str_extract(x,"[a-zA-Z]+$")

EDIT: one could use lookaround feature as well: str_extract(x,"(?=_*)[a-zA-Z]+$")

also from baseR

regmatches(x,gregexpr("[a-zA-Z]+$",x))[[1]]

From documentation ?regex:

The caret ^ and the dollar sign $ are metacharacters that respectively match the empty string at the beginning and end of a line.

PKumar
  • 10,971
  • 6
  • 37
  • 52
  • If the end of the string could also be something like "subvar4" this approach would need a little modification, right? – talat May 08 '17 at 15:21
  • When I modify the search string as `y = "string__subvar1__subvar2_subvar3_myvar"` the result is also "myvar" - but in this case it has not split on the "__" string. – Henk May 08 '17 at 15:23
  • `str_extract(x,"(?=_*)[a-zA-Z]+$")` what about this, @Henk sorry brother, I never got from your question that you need split via _ (underscore) – PKumar May 08 '17 at 15:26
  • No worries, I should have made this clearer and edited the question. This code works. – Henk May 08 '17 at 15:30
1

Could this work? Sorry, I hope i'm understanding what you're asking correctly.

substr(x,gregexpr("_",x)[[1]][length(gregexpr("_",x)[[1]])]+1,nchar(x))
[1] "myvar"
kostr
  • 836
  • 7
  • 13
  • Yes, you understood exactly right. It works...I just wished this was a little easier. – Henk May 08 '17 at 15:26