Hey, I was wondering how I can find a Street Address in a string in Python/Ruby?
Perhaps by a regex?
Also, it's gonna be in the following format (US)
420 Fanboy Lane, Cupertino CA
Thanks!
Using your example this is what I came up with in Ruby (I edited it to include ZIP code and an optional +4 ZIP):
regex = Regexp.new(/^[0-9]* (.*), (.*) [a-zA-Z]{2} [0-9]{5}(-[0-9]{4})?$/)
addresses = ["420 Fanboy Lane, Cupertino CA 12345"]
addresses << "1829 William Tell Oveture, by Gioachino Rossini 88421"
addresses << "114801 Western East Avenue Apt. B32, Funky Township CA 12345"
addresses << "1 Infinite Loop, Cupertino CA 12345-1234"
addresses << "420 time!"
addresses.each do |address|
print address
if address.match(regex)
puts " is an address"
else
puts " is not an address"
end
end
# Outputs:
> 420 Fanboy Lane, Cupertino CA 12345 is an address
> 1829 William Tell Oveture, by Gioachino Rossini 88421 is not an address
> 114801 Western East Avenue Apt. B32, Funky Township CA 12345 is an address
> 1 Infinite Loop, Cupertino CA 12345-1234 is an address
> 420 time! is not an address