I have a string which includes square brackets, sometimes this is a set of double square brackets and sometimes a set of single followed by a double set. My issue my issue here is that when i try to use gsub to remove these I can’t seem to remove both sets, depending on my code i either remove the single or the double set.
df$names
Name, Example [A] [[UK
Name2, AnotherExample [[USA
What I am trying to achieve:
df$names
Example Name
AnotherExample Name
To do this I have tried the following; (the top option removes the double square brackets whilst the bottom line removes the single)
gsub("(.+)\\, (.+) \\[.+", "\\2 \\1", df$names)
gsub("(.+)\\, (.+)", "\\2 \\1", df$names)
>Solution :
In the second replacement, match anything except a bracket to make the query less "greedy".
gsub("^(.+),[[:space:]]+([^\\[]+).*$", "\\2 \\1",df$names)