Search code examples
rgsub

Gsub with multiple special characters


I have the following string with multiple special characters and I'm struggling to get out the first part due to the commas. Here is an example (just dummy data) where I am trying to get out everything before the 1st comma which is the stadium. My issue is that I think when using .* it always looks for the last instance of this in the string? Also, when using a question mark to try and combat this Im still have no luck. I have attached my attempt below the string

mystring
"Wembley Stadium, South Way, London, HA9 0WS#100000, 1000000"

my atttempt;
gsub("(.*)\\, .*?", "\\1", mystring)

Here I am trying to stay look for everything up until the first comma, then everything after the comma, using the brackets to indicate that first part is what I want to keep


Solution

  • Is this your goal?

    mystring <- "Wembley Stadium, South Way, London, HA9 0WS#100000, 1000000"
    gsub("([^,]+),.*", "\\1", mystring)
    
    "Wembley Stadium"