Parsing street addresses in Ruby

前端 未结 5 1854
萌比男神i
萌比男神i 2021-02-09 18:08

I am processing addresses into their respective field format for the database. I can get the house number out and the street type but trying to determine best method to get the

5条回答
  •  萌比男神i
    2021-02-09 18:43

    You can play fast and loose with named capture groups in a regex

    matches = res[:address].match(/^(?\S*)\s+(?.*)\s+(?.*)$/)
    number = matches[:number]
    house = matches[:name]
    street_type = matches[:type]
    

    or if you wanted your regex to be a little more accurate with the type you could replace (?.*) with (?(Blvd|Ave|Rd|St)) and add all the different options you'd want

提交回复
热议问题