nonegiven72 nonegiven72 - 6 months ago 16
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.

Thanks!

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

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"
Comments