0

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)

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
  • try this instead of `|.*`: `\|.*$` – Kaus2b Sep 26 '21 at 21:46
  • What's the website? There may be a better way to get the string you are after. How are you getting the result you show above? – QHarr Sep 26 '21 at 22:57
  • Actually, this is one of the names of the phone I found when I search "Realme Mobile Phones" on amazon. in. There only a few names are in that form. For more details you can visit: https://www.amazon.in/s?k=Realme+Mobile+Phones&ref=nb_sb_noss_2 – Debojit Roy Sep 26 '21 at 23:13
  • you may see this post for idea. https://stackoverflow.com/questions/46661012/r-remove-string-before-delimiter – Peter Chung Sep 27 '21 at 03:13

1 Answers1

0

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)"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213