我只想将此文本文件分割成行并对行进行分类。如果该行以“数量”开头,则下一行是订单商品,直到该行以“GST”开头。
如果该行以“总金额”开头,则这是总金额行。
Business me . ' l Address "rwqagePnnter Pro DemcRafifilp Address "mfgr Eva|uat|on Only Contact line 1 Transaction Number 10006 Issue Date 27/02/201 Time 10:36:55 Salesperson orsa orsa Qty Description Unit Price Total 1 test $120.00 $120.00 GST $10.91 Total Amount $120.00 Cash $120.00 Please contact us for more information about this receipt. Thank you for your business. d . test
请告诉我如何处理PegJS http://pegjs.majda.cz/
答案 0 :(得分:6)
这是一个快速而肮脏的样本解决方案
{
var in_quantity = false // Track whether or not we are in a quantity block
var quantity = []
var gst = null
var total = null
}
start =
// look for a quantity, then GST, then a total and finally anything else
(quantity / gst / total / line)+
{
return {quantity: quantity, gst: gst, total: total}
}
chr = [^\n]
eol = "\n"?
quantity = "Qty" chr+ eol { in_quantity = true; }
gst = "GST" g:chr+ eol { in_quantity = false; gst = g.join('').trim(); }
total = "Total Amount" t:line { in_quantity = false; total = t.trim(); }
line =
a:chr+ eol
{
if( in_quantity ){
// break quantities into columns based on tabs
quantity.push( a.join('').split(/[\t]/) );
}
return a.join('');
}
答案 1 :(得分:3)
以下代码如何作为另一种解决方案。
{
var result = [];
}
start
= (!QTY AnyLine /
set:(Quantities TotalAmount)
{result.push({orders:set[0], total:set[1]})}
)+ (Chr+)?
{return result;}
QTY = "Qty"
GST = "GST"
Quantities
= QtyLine order:(OrderLine*) GSTLine {return order;}
QtyLine
= QTY Chr* _
OrderLine
= !GST ch:(Chr+) _ {return ch.join('');}
GSTLine
= GST Chr* _
TotalAmount
= "Total Amount" total:(Chr*) _ {return total.join('');}
AnyLine
= ch:(Chr*) _ {return ch.join('');}
Chr
= [^\n]
_
= "\n"
答案 2 :(得分:0)
您可以使用XML,或者您可以使用“/”结束每一行,然后使用拆分函数将它们拆分。
mytext = mytext.split("/");
然后使用它。我不知道为什么你不会只使用sql或类似的东西。