I need extract a part of a string from a address variable. My data looks like this
"
[45] "Matara Road, Habaraduwa | Talpe, Unawatuna, Galle GL 80630, Sri Lanka "
[46] "Jungle Beach Road, Buonavista | Rumassala, Unawatuna, Galle 80600, Sri Lanka "
[47] "10 Church Street | inside the Fort, Galle, Sri Lanka "
[48] "78 Mile Post Matara Road Mihiripenna, Unawatuna, Galle 80615, Sri Lanka "
[49] "No: 288 Galle Road | Dadella, Galle 80000, Sri Lanka "
[50] "Matara Road, Koggala, Galle, Sri Lanka "
I want to extract the city from this string, which in this case should be "Galle". The only pattern I can think of is that it appears before "Sri lanka". Or the city is in between "," and ", Sri Lanka". Here is the code that I used
gsub("\\.s*|(, Sri Lanka).*", "", a)
However using this code I get the following results.
[45] "Matara Road, Habaraduwa | Talpe, Unawatuna, Galle GL 80630"
[46] "Jungle Beach Road, Buonavista | Rumassala, Unawatuna, Galle 80600"
[47] "10 Church Street | inside the Fort, Galle"
[48] "78 Mile Post Matara Road Mihiripenna, Unawatuna, Galle 80615"
[49] "No: 288 Galle Road | Dadella, Galle 80000"
[50] "Matara Road, Koggala, Galle"
Is there anyway to keep only the city