Truncate the string in R
I am trying to extract part of a string in R. The strings are shaped ABC_constantStuff_ABC_randomStuff
and ABC
is what I am trying to extract. ABC
unknown and can be 1-3 characters long. I tried grep
and gsub
but don't know how to specify my regex using
str <- 'GDP\" title=\"GDP - News\"></a>"'
symbol <- gsub(pattern,'',str)
It is GDP
unknown here and can be 1-3 characters long, \" title=\"
is constant in every line and I would like to remove\" title=\"GDP - News\"></a>"
Thank you for your help.
Simple is
R> gsub("^([A-Z]*)_.*", "\\1", "ABC_constantStuff_ABC_randomStuff")
[1] "ABC"
R>
which gets all letters up to the first _
.
Another helper _
is your separator
R> strsplit( "ABC_constantStuff_ABC_randomStuff", "_")[[1]][c(1,3)]
[1] "ABC" "ABC"
R>
Does it help?
> sub("\".*$", "", str)