c# - Splicing price from amount on concatenated string -
i working on pos system. version replace existing version in use. version in use programmed in visual basic , has loads of obsolete , unused classes , screens, whereas new version use visual c# (winforms). looking way receipts list (where product contains name, price , amount). however, older receipts stored single databasecell follows:
title of product a€ 449,001€ 449,00title of product b€ 14.2€ 28.00title of product c€0.52 €1title of product d€220 €40
so far, i've been able slice these single strings of 1 product using regex.replace(text, @"(€\d*.?\d{0,2})\s*(\d+)\s*(€\d*.\d{0,2})", "${0}↨")
, slicing afterwards (which done within 1 go probably, find more clear)
string pattern = "(.*?)↨"; matchcollection matches = regex.matches(text, pattern); foreach (match match in matches) { stringlist.add(match.groups[0].value); }
which result in list of strings containing [title, price/per item , amount , total price item]:
title of product a€449,001 €449,00 title of product b€14.2 €28.00 title of product c€0.52 €1 title of product d€220 €40
i use (assume product declared)
matchcollection matches = regex.matches(oneproducttext, @"(.*?)€(\d*.?\d{0,2})\s*([1-9]+\d*)\s*€(\d*.\d{0,2})↨"); //text - price/per - amount - price total || use after broken pieces. foreach (match match in matches) { product = new dckkassa.product(); product.name = match.groups[1].value; product.price = decimal.parse(match.groups[2].value, cultureinfo.invariantculture); product.amount = int.parse(match.groups[3].value); }
which work strings product conversions, there corner cases not handled. looking way handle those. strings containing:
€.011 €0.01 - handled €1.001 €1.00 - handled €1.01 €1.00 - handled €11 €1.00 - handled €1.0011 €11.00 - handled €1.010 €11.00 - handled €1.011 €11.00 - unhandled result in €1.01 being handled 1 time €1.11 €11.00 - unhandled result in €1.10 being handled 1 time €112 €12.00 - unhandled result in €11 being handled 2 times etc.
so i'm struggling looking way €1.011 €11.00
split in €1.0 | 11 | €11.00.
thinking of taking latter price, , trying find right cut between first euro-amount combination such firsteuro * amount results in second amount. however, wondering if there hidden (well @ least me) feature in regex handle such thing.
i love hear suggestions.
try following :
{ string input = "title of product a€ 449,001€ 449,00title of product b€ 14.2€ 28.00title of product c€ €0.52 €1title of product d€ €220 €40"; string pattern = "(?'title'title of product [a-z]+)&(?'currency'[^;]+);\\s+(?'value'[€0-9,\\. ]+)"; matchcollection matches = regex.matches(input,pattern); foreach (match match in matches) { console.writeline("title = '{0}', currency = '{1}', value = '{2}' ", match.groups["title"].value, match.groups["currency"].value, match.groups["value"].value); } console.readline();
Comments
Post a Comment