How to check keywords in reversed order from string

I have 200+ countries with keywords.
Please find some sample keywords.

Expand Table

India IN,Mumbai,Delhi,Mysore,Surat,Kolkata,Punjab,Bangalore
Iraq IQ,BAGHDAD,BASRA,NAFJAF,KIRKUK,KARBALA,NASIRIYAS,AMARA
China CN,SHANGHAI,beijing,chongqing,guangzhou,tianjin,shenzhen,hangzhou,wuhan
France FRA,Paris,Lyon,Nice,Nantes,Reims,Lille
England UK,London,Manchester,Bristol,Liverpool,Southampton
Russia Moscow,Kazan,Omsk,Volgograd,Krasnoyarsk,Samara
Afghanistan AF,Kabul,kandahar,jalalabad,kandahar,herat,ghazni,khanabad
Zimbabwe Harare, Bulawayo, Manicaland, Midlands, Mashonaland West, Masvingo
Germany Berlin,Munich,Hamburg,stuttgart,essen,Leipzig
Morocco MA,Casablanca,Rabat,fez,Tangier,Agadir,kenitra,oujda,meknes
Pakistan Karachi,Lahore,Punjab,Islamabad,Hyderabad,Faisalabad,Peshawar,Multan,Bahawalpur,Sargodha
Jamaica JM,Kingston,Portmore,Montego,saint catherine,mandeville,maypen,holdharbour
Mali bamako,gao,sikasso,kalabancoro,koutiala,segou,kayes,kati,mopti,niono
Iran Hamadan, Ilam,Karaj, Tehran, East Azerbaijan, Semnan, Birjand, Bushehr
Spain Madrid,Barcelona,Seville,bilbao.valencia,cadiz,Girona

I have given countries keywords with name in dictionary (key,value).

So i want to check address string from backside i e. Last word first to find accurate Country.
Basically I want to check string in reverse order if multiple country keywords found then it should return last keyword country name.

Input example 1:
“786 apartment, Shanghai building, Mumbai 400710”

It contains 2 keywords - 1 from (Shanghai)china and 1 from (Mumbai) India

Output:
India(Mumbai)

Input example 2:
" Abc Kingston apartment, near Uk ambassy , Madrid 7896 ES"

It contains 3 keywords- 1 from Jamaica (Kingston), 1 from England (UK) and 1 from Spain(Madrid)

Output:
Spain

1 Like

Also input 3:

“Xyz apart china cluster H7 United Arab Emirates”

Here: 2 Keywords found 1 from China(china) and other from United Arab Emirates (United Arab Emirates).

Output:
United Arab Emirates

So basically It should also identify more than 1 word keywords.

Hi @Mansi_Mhatre

This is interesting !!

you can do this with regular expression

  1. \w+(?=\s+[0-9])
  2. (?<=\d+ ).+(?= [A-Z]+)

First expression will identify the city for your 1st two examples. But will not identify anything from 3rd example - this will programmatically help to switch to 2nd regex to identify the city name from that address format.

However, I suspect that there will be few outlier and you might need to fine tune these expressions further

hope this is helpful