Home > Software design >  How do I extract location names from a string with mixed commas and quotation marks? (using Regex or
How do I extract location names from a string with mixed commas and quotation marks? (using Regex or

Time:08-13

I have a string of locations

locations = 'Los Angeles California ,"Heliopolis, Central, Cairo, Egypt",Berlin Germany, Paris France," Cairo, Egypt " , "Dokki, Giza, Egypt " , Singapore'

Note that the location names are separated by commas. But for each name with commas in between, it is enclosed in double quotation marks. Also there are prefix/suffix white spaces to be stripped.

After extracting the names into a list, the result should be:

['Los Angeles California', 'Heliopolis, Central, Cairo, Egypt', 'Berlin Germany', 'Paris France', 'Cairo, Egypt', 'Dokki, Giza, Egypt', 'Singapore']

I have tried this and it is able to get the results. But I'm laughing at my work because it looks so cumbersome

  • Related