正则表达式解析地址

时间:2016-09-01 15:17:36

标签: ruby regex gsub

我正在尝试学习如何使用正则表达式来解析位置/地址字符串。 不幸的是,我给出的数据与大多数地址的编写方式不一致和不同步。下面是我到目前为止,我遇到的问题是我需要多次解析字符串以使其达到正确的格式。

以下面的字符串为例:102 Spruce, 108 Spruce, 110 Spruce, Greenwood, SC 29649我想要的最终结果是110 Spruce, Greenwood, SC 29649

CODE:

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

3 个答案:

答案 0 :(得分:2)

这是其中之一,而不仅仅是一种方式。这是另一个:

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"

答案 1 :(得分:1)

假设格式为:

Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUR_OF_DAY);
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);

if(hours*3600 + minutes*60 + seconds < 1800){
    // Day changed since last task
}

然后你只需要使用美元符号锚定到字符串的末尾来获取最后3个部分:

"Stuff you aren't interested in, more stuff, more stuff, etc., house, city, state zip"

答案 2 :(得分:0)

没有正则表达式的尝试:

address = "102 Spruce, 108 Spruce, 110 Spruce, Greenwood, SC 29649"
elements = address.split(",").map(&:strip)
city, state_and_zip = elements[elements.length-2..-1]
addresses = elements[0...elements.length-2]

p [addresses.last, city, state_and_zip].join(",")

输出:

"110 Spruce,Greenwood,SC 29649"