2014-01-23 71 views
0
陣列

我有以下地址:Tokenise詞到通過正則表達式

1330城市街道,PO BOX 444 ---零件7,8,9和13,~~~中心惠靈頓鄉(村的Elora), 惠靈頓縣

我想標記每個以逗號結尾的單詞。在上面的例子中,我們將tokenise如下:

1330城市街道
PO BOX 444 ---配件7
和13
~~~中心惠靈頓鄉(村Elora)

我想用紅寶石做。任何幫助將不勝感激

回答

0

這一個可以得心應手了。它在逗號後刪除最後一個標記,然後執行拆分。

input = '1330 City Street, PO BOX 444 --- Parts 7,8,9, and 13,~~~Centre Wellington Township (Village of Elora), County of Wellington' 
print input.gsub(/[^,]*$/, "").split(/,/) 
0

你可以這樣做:

s = "1330 City Street, PO BOX 444 --- Parts 7,8,9, and 13,~~~Centre Wellington Township (Village of Elora), County of Wellington" 

s.split(',').last.strip # => "County of Wellington" 
0

我會做如下:

s = "1330 City Street, PO BOX 444 --- Parts 7,8,9, and 13,~~~Centre Wellington Township (Village of Elora), County of Wellington" 
s.scan(/[^,]+(?=[,])/).map(&:strip) 
# => ["1330 City Street", 
#  "PO BOX 444 --- Parts 7", 
#  "8", 
#  "9", 
#  "and 13", 
#  "~~~Centre Wellington Township (Village of Elora)"] 
puts s.scan(/[^,]+(?=[,])/).map(&:strip) 

# >> 1330 City Street 
# >> PO BOX 444 --- Parts 7 
# >> 8 
# >> 9 
# >> and 13 
# >> ~~~Centre Wellington Township (Village of Elora) 

檢查了這一點也是在Rubular.com