Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is this your goal?

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

"Wembley Stadium"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading