我正在尝试学习如何使用regex解析位置/地址字符串。
不幸的是,我得到的数据与大多数地址的书写方式不一致,也不符合常规。下面是我目前所拥有的,我所面临的问题是,我需要多次解析字符串,以使其降到正确的格式。
以下面的字符串为例:102 Spruce, 108 Spruce, 110 Spruce, Greenwood, SC 29649
我想要的最终结果是110 Spruce, Greenwood, SC 29649
代码:
l = nil
location_str = "102 Spruce, 108 Spruce, 110 Spruce, Greenwood, SC 29649"
1.upto(4).each do |attempt|
l = Location.from_string(location_str)
puts "TRYING: #{location_str}"
break if !l.nil?
location_str.gsub!(/^[^,:\-]+\s*/, '')
end
输出:
TRYING: 102 Spruce, 108 Spruce, 110 Spruce, Greenwood, SC 29649
TRYING: , 108 Spruce, 110 Spruce, Greenwood, SC 29649
TRYING: , 108 Spruce, 110 Spruce, Greenwood, SC 29649
TRYING: , 108 Spruce, 110 Spruce, Greenwood, SC 29649
预期:
TRYING: 102 Spruce, 108 Spruce, 110 Spruce, Greenwood, SC 29649
TRYING: 108 Spruce, 110 Spruce, Greenwood, SC 29649
TRYING: 110 Spruce, Greenwood, SC 29649
最佳答案
这是一个有不止一种方法可以做的事情还有一个:
def address_from_location_string(location)
*_, address, city, state_zip = location.split(/\s*,\s*/)
"#{address}, #{city}, #{state_zip}"
end
address_from_location_string("102 Spruce, 108 Spruce, 110 Spruce, Greenwood, SC 29649")
# => "110 Spruce, Greenwood, SC 29649"