Bonono Bonono - 2 months ago 6
R Question

Substring character string based on a certain character in string

I have a vector with several elements as character strings split by

/
:

aa <- c("Session/1/Focal_011/Dave_1", "Session/2/Focal_223/Dave_2",
"Session/3/Focal_23/Quentin_3", "Session/4/Focal_4/Gerald_4",
"Session/5/Focal_521/Pedro_5")


All I want to keep from each element is
Name_n
where
n = element number
.

Can I use
substr
depending on the
/
character in the string


Using
substr(aa, 21, nchar(aa))
I get

[1] "Dave_1" "Dave_2" "uentin_3" "rald_4" "Pedro_5"


This is because the character strings are of different
length
.

How can I standardise it so I just keep from the last
/
?

Using
regex
? If so, how?

Answer

Use

> aa <- c("Session/1/Focal_011/Dave_1", "Session/2/Focal_223/Dave_2", "Session/3/Focal_23/Quentin_3", "Session/4/Focal_4/Gerald_4", "Session/5/Focal_521/Pedro_5")
> sub("^.*/(.*)$", "\\1", aa)
[1] "Dave_1"    "Dave_2"    "Quentin_3" "Gerald_4"  "Pedro_5"  

See the online demo.

The ^.*/(.*)$ will match any 0+ chars up to the last / and the slash, and then will capture the rest into Group 1 that will be restored with the \1 backreference in the replacement pattern.