我的影片格式這樣的數據,作爲一個字符串:Ruby的正則表達式匹配字符模式和換行之間子
"1. Enloe Medical Center - 2,000
2. CSU Chico - 1,805
3. Walmart Distribution Center - 1,350
4. Pacific Coast Producers (Agribusiness) - 1,200
5. Marysville School District - 1,000
6. Feather River Hospital - 865
7. Sunsweet Growers (Agriculture) - 600
8. YRC (Freight Services) - 500
9. Sierra Pacific Industries (Lumber Products) - 500
10. Colusa Casino Resort - 500"
在Ruby應用程序,我想創建兩個數組:的一個每個編號列表標記和儀表板,以及包含儀表板和換行符(爲整數)之間的數字的其中一個子之間的串,就像這樣:
labels = ["Enloe Medical Center","CSU Chico","Walmart Distribution Center","Pacific Coast Producers (Agribusiness)","Marysville School District","Feather River Hospital","Sunsweet Growers (Agriculture)","YRC (Freight Services)","Sierra Pacific Industries (Lumber Products)","Colusa Casino Resort"]
numbers = [2000, 1805, 1350, 1200, 1000, 865, 600, 500, 500, 500]
我不是我的正則表達式如此之大;我知道如何做替代和匹配,但我不確定從哪裏開始。誰能幫忙?
我喜歡你的方法真的很不錯,用一個更短的正則表達式把它變成一個單線程:-) labels,numbers = str.scan(/ \ d + \。\ s(。+)\ s- \(\ d。 *)$ /)。map {| label,number | [標籤,數字.gsub(「,」,「」).to_i]} .transpose – bjhaid
@bjhaid我認爲你可以使用像這樣的東西寫得更少:'/ \ $(? \ d +)\。(? \ d +)/ =〜「$ 3.67」; #=> 0; dollars#=>「3」' –
@DarekNędza我很困惑你的例子與討論中的問題無關 – bjhaid