fork download
  1. import re
  2.  
  3. def parse_results(string):
  4. space = r"\s{0,5}"
  5. building_type = r"(?:[Uu]nit|[Ss]tudio|[Ff]lat)?"
  6. street_type = (r"\d+[&-]*\w*\s*"
  7. r"(?:[Ee]nd|[Gg]reen|[Cc]auseway|[Cc]heapside|[Cc]rescent|"
  8. r"[Ss]treet|[Ll]ane|[Ww]alk|[Rr]oad|[Aa]venue|[Dd]rive|"
  9. r"[Pp]ark|[Ww]ay|[Pp]lace|[Pp]arade|[Ii]ndustrial"
  10. r"[Ee]state|[Tt]rading [Ee]state|[Hh]ouse|[Gg]reen)")
  11. postcode = r"[A-Z0-9][A-Z0-9][A-Z0-9]?[A-Z0-9]? {1,2}[0-9][A-Z]{2}"
  12. pattern = re.compile(rf"{building_type}{space}{street_type}(?:\s{{1,5}}\w+){{0,5}}"
  13. rf"{space}{postcode}")
  14. print(pattern.pattern)
  15. try:
  16. return [re.sub(r"\s+", r" ", x) for x in pattern.findall(string)]
  17. except Exception as e:
  18. return (f"Error looking for address, exception {e}")
  19.  
  20. print(parse_results('Unit 23 End AS4 0SS'))
Success #stdin #stdout 0.02s 9792KB
stdin
Standard input is empty
stdout
(?:[Uu]nit|[Ss]tudio|[Ff]lat)?\s{0,5}\d+[&-]*\w*\s*(?:[Ee]nd|[Gg]reen|[Cc]auseway|[Cc]heapside|[Cc]rescent|[Ss]treet|[Ll]ane|[Ww]alk|[Rr]oad|[Aa]venue|[Dd]rive|[Pp]ark|[Ww]ay|[Pp]lace|[Pp]arade|[Ii]ndustrial[Ee]state|[Tt]rading [Ee]state|[Hh]ouse|[Gg]reen)(?:\s{1,5}\w+){0,5}\s{0,5}[A-Z0-9][A-Z0-9][A-Z0-9]?[A-Z0-9]? {1,2}[0-9][A-Z]{2}
['Unit 23 End AS4 0SS']