Gsub with multiple special characters

39 views Asked by At

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

2

There are 2 answers

1
Isaac On BEST ANSWER

Is this your goal?

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

"Wembley Stadium"
0
geek45 On

I think you have to use (.*?) to capture any character until the first comma.

result <- gsub("^(.*?),.*$", "\\1", mystring)

> print(result)
[1] "Wembley Stadium"