1

I have the following string, named ID:

ID <- c("SONY (~U$) (#T) - SONY (~U$) (#T)", 
        "KDDI (~U$) (#T) - KDDI (~U$) (#T)", 
        "SOFTBANK (~U$) (#T) - SOFTBANK (~U$) (#T)")

The result that I want is:

ID <- c("SONY", "KDDI", "SOFTBANK")

I want to do this using gsub in R, but I have no idea how to do this. Any help or advice is appreciated.

So far, I have only tried:

gsub("^ (~U$) (#T) -", "", names_P , fixed = TRUE)

Which results in:

ID <- c("SONY SONY", "KDDI KDDI", "SOFTBANK SOFTBANK")
AmarS96
  • 43
  • 5
  • Die you want to write: `ID <- c("SONY (~U$) (#T) - SONY (~U$) (#T)", "KDDI (~U$) (#T) - KDDI (~U$) (#T)", "SOFTBANK (~U$) (#T) - SOFTBANK (~U$) (#T))")`? The second and third string seem to be combined unintentionally. ` – JBGruber Mar 02 '20 at 12:09
  • Hi JBGruger, thanks for letting me know. That was indeed unintentional and has been corrected now. – AmarS96 Mar 02 '20 at 12:12
  • What have you tried so far, can you give some examples of code / thoughts – sorearm Mar 02 '20 at 12:15

2 Answers2

0

Assuming that what you meant is:

ID <- c("SONY (~U$) (#T) - SONY (~U$) (#T)", "KDDI (~U$) (#T) - KDDI (~U$) (#T)", "SOFTBANK (~U$) (#T) - SOFTBANK (~U$) (#T))")

You can use a backreference here:

gsub("(^[[:alpha:]]+).+", "\\1", ID)
#> [1] "SONY"     "KDDI"     "SOFTBANK"

^[[:alpha:]]+ means the first alphabetic sequence of characters in the string. Wrapping it in () makes it referenceable. .+ outside the bracket means everything else. So the string inside the bracket + outside is replaced by only the string inside the bracket.

Check out this cheatsheet for info.

JBGruber
  • 11,727
  • 1
  • 23
  • 45
  • Hi JBGruber, thank you so much for your time and effort. It did the job. I am unable to upvote your answer, but I really appreciate your help. – AmarS96 Mar 02 '20 at 12:17
  • You can accept this as the correct answer if it solves your problem. – JBGruber Mar 02 '20 at 12:18
0
gsub("(\\S+).+", "\\1", ID)
#"SONY"     "KDDI"     "SOFTBANK"

\\S matches any non-space \\1 returns the first (and only here) captured group (...)

Edward
  • 10,360
  • 2
  • 11
  • 26