0
我有這樣一個文件:Ruby檢查文件格式的有效方法是什麼?
Fruit.Store={
#blabla
"customer-id:12345,item:store/apple" = (1,2); #blabla
"customer-id:23456,item:store/banana" = (1,3); #blabla
"customer-id:23456,item:store/watermelon" = (1,4);
#blabla
"customer-id:67890,item:store/watermelon" = (1,6);
#The following two are unique
"customer-id:0000,item:store/" = (100, 100);
#
"" = (0,0)
};
除註釋,每一行具有相同的格式:customer-id
和item:store/
是固定的,並且customer-id
是一個5位數字。最後兩條記錄是唯一的。我怎樣才能確保文件的格式正確?我正在考慮使用第一條特殊行Fruit.Store={
的標誌,並且對於下面的行用「,」和「=」分隔每一行,如果分割的行不正確,則將它們與最後兩條記錄進行匹配。我想爲它使用Ruby。有什麼建議?謝謝。
我也想使用正則表達式的格式,並寫道:
^"customer:\d{5},item:store\/\D*"=\(\d*,\d*\);
,但我想這兩種情況結合起來(有評論和不加評論):
^"customer:\d{5},item:store\/\D*"=\(\d*,\d*\);$
^"customer:\d{5},item:store\/\D*"=\(\d*,\d*\);#.*$
如何我能做到嗎?謝謝
對於'item:store /'的每個值,需要考慮的限制/規則如何? – Gerry
而不是發明自己的格式,爲什麼不使用像YAML,JSON或類似的東西? – tadman
「item:store /」已修復,謝謝@Gerry – user8142520