I have a string where I wish to extract the country code, this will always be in the form of capital letters with 3 characters.
mystring
"Bloggs, Joe GBR London (1)/Bloggs, Joe London (2)"
"Bloggs, Joe London (1)/Bloggs, Joe GBR London (2)"
"Bloggs, Joe London (1)/Bloggs, Joe London (2)"
"Bloggs, Joe GBR London (1)/Bloggs, Joe GBR London (2)"
"Bloggs, J-S GBR London (1)/Bloggs, J-S GBR London (2)"
What im trying to get
mystring
GBR/
/GBR
/
GBR/GBR
GBR/GBR
Blanks are fine if there is no country, I can deal with them
I’ve tried a couple of things which I have seen on here, one which tried to remove all characters that aren’t capital but then I am left with other letters which i don’t want like the capitals from the name and location. I then tried to do similar by trying to remove all letters that don’t start and end with a capital (also had no joy due to name issues);
gsub("[^A-Z$]", "", mystring)
If I just keep all capital letters where there are 3 letter that might work, but I cant quite get the code right, i think it would look something like below if anyone know or even knows a more robust method;
gsub("[^A-Z$]{3}", "", mystring)