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.

+3


source to share


2 answers


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> 

      

+4


source


Does it help?



> sub("\".*$", "", str)

      

+3


source







All Articles