Note that there are some explanatory texts on larger screens.

plurals
  1. PORegular expressions in Python. Can anybody help a newbie?
    primarykey
    data
    text
    <p>This simple problem is kiling me. I posted something earlier about trying to clean up a database of addresses and somebody suggested GeoPy to check the validity of the addresses. Great tool which I did not know, but before doing that, I need to clean up the database a little bit, since geopy will not deal with messy formatting. The solution is to use regular expressions, which I think I have sort of fixed for most of the types of addresses I seen in the database. Nevertheless, I am having problems with the last RegExp I defined (called r4 in the code), because it is retuning part of the first parenthesis which I don't need, and I don't know why I have a extra white spaces when it returns the last group (City: London, Country: England). Can anybody help?</p> <pre><code>import re r1 = '\s*ForeignZip.*--\s*([\d\.]+)' r2 = '(\w+)\W*,\W*(\w*)' r3 = '(?&lt;=\().*?(?=\))' r4 = '(\w+\W\()' Location = [' ForeignZip (xxx) -- 734.450','Washington, DC.','London (England)'] for item in Location: print item match1 = re.search(r1,item) match2 = re.search(r2,item) match3 = re.search(r3,item) match4 = re.search(r4,item) if match1: print 'pattern 1 found:', match1.group(1) elif match2: print 'pattern 2 found: City :' + match2.group(1) + ", State :" + match2.group(2) elif match3: print 'pattern 3 found: City: ', match4.group() + ", Country :" + match3.group(0) else: print 'no match' </code></pre> <p>This returns</p> <pre><code> ForeignZip (xxx) -- 734.450 pattern 1 found: 734.50 Washington, DC. pattern 2 found: City :Washington, State :DC London (England) pattern 3 found: City: London (, Country :England </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload