In my web scraping exercise, I have come up with a peculiar form of string given by -
st<-c("Samsung Galaxy M21 2021 Edition (Charcoal black , 6GB RAM, 128GB Storage) | FHD sAMOLED | 6 Months Free Screen Replacement for Prime")
And I want the part of the string before "|"
i.e., I want the output as:
"Samsung Galaxy M21 2021 Edition (Charcoal black , 6GB RAM, 128GB Storage)"
I am trying as s <- st %>% str_replace("|.*","")
but didn't get any improvement over "st"(defined above)
CodePudding user response:
You can try -
st<-c("Samsung Galaxy M21 2021 Edition (Charcoal black , 6GB RAM, 128GB Storage) | FHD sAMOLED | 6 Months Free Screen Replacement for Prime")
sub('\\s*\\|.*', '', st)
#[1] "Samsung Galaxy M21 2021 Edition (Charcoal black , 6GB RAM, 128GB Storage)"