nonegiven72 nonegiven72 - 1 year ago 89
Ruby Question

Removing Street Suffix from Address

Trying to identify(and Remove) street suffixes (like "St", "Dr", etc...) from addresses. Assume that the suffixes are uniform and that we can create a comprehensive list of them.


street_suffix_list = ["St", "Dr", "Ave", "Blvd", "Tr"]
address = "105 Main St"

#returns "Main St"
street = address.gsub(/^((\d[a-zA-Z])|[^a-zA-Z])*/, '')

#desired: "St"
street_suffix =

#desired: "Main"
street_name =

Answer Source

You just need to separate street from suffix with Regexp:

street_suffix_list = ["St", "Dr", "Ave", "Blvd", "Tr"]
address = "105 Main St"

idx = /(#{street_suffix_list.join('|')})\z/ =~ address
# $1 => St
sfx = $1
street = address[0..idx-1].strip
# street => "105 Main"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download