For example using gsub
or sub
gsub('.*:(.*)','\\1',string)
[1] "E001" "E002" "E003"
If you are using data.table
then tstrsplit()
is a natural choice:
tstrsplit(string, ":")[[2]]
[1] "E001" "E002" "E003"
The unglue package provides an alternative, no knowledge about regular expressions is required for simple cases, here we'd do :
# install.packages("unglue")
library(unglue)
string = c("G1:E001", "G2:E002", "G3:E003")
unglue_vec(string,"{x}:{y}", var = "y")
#> [1] "E001" "E002" "E003"
Created on 2019-11-06 by the reprex package (v0.3.0)
More info : https://github.com/moodymudskipper/unglue/blob/master/README.md
This should do:
gsub("[A-Z][1-9]:", "", string)
gives
[1] "E001" "E002" "E003"
Late to the party, but for posterity, the stringr package (part of the popular "tidyverse" suite of packages) now provides functions with harmonised signatures for string handling:
string <- c("G1:E001", "G2:E002", "G3:E003")
# match string to keep
stringr::str_extract(string = string, pattern = "E[0-9]+")
# [1] "E001" "E002" "E003"
# replace leading string with ""
stringr::str_remove(string = string, pattern = "^.*:")
# [1] "E001" "E002" "E003"
Another method to extract a substring
library(stringr)
substring <- str_extract(string, regex("(?<=:).*"))
#[1] "E001" "E002" "E003
(?<=:)
: look behind the colon (:
)Here is another simple answer
gsub("^.*:","", string)
Source: Stackoverflow.com