拼接字符串

时间:2017-04-07 14:26:02

标签: c# regex

我正在研究POS系统。此版本将替换当前使用的现有版本。正在使用的版本是在Visual Basic中编写的,并且有大量现在过时和未使用的类和屏幕,而这个新版本将使用Visual C#(WinForms)。我正在寻找一种方法将收据存入List(产品包含名称,价格和金额)。但是,较旧的收据存储在单个数据库单元中,如下所示:

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

到目前为止,我已经能够使用Regex.Replace(text, @"(€\d*.?\d{0,2})\s*(\d+)\s*(€\d*.\d{0,2})", "${0}↨")将这些切成一个产品的单个字符串并在之后进行切片(这可能在一次性内完成,但我发现这更清楚了)

string pattern = "(.*?)↨";
MatchCollection matches = Regex.Matches(text, pattern);
foreach (Match match in matches)
{
    stringList.Add(match.Groups[0].Value);
}

然后会产生一个字符串列表,其中包含[标题,价格/每件商品,此商品的金额和总价格]:

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

然后我使用(假设产品被宣布)

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 into 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);
        }

哪个适用于大多数字符串进行产品转换,但有些角落情况无法处理。我一直在寻找一种方法来处理这些问题。 字符串包含:

€.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 will result in €1.01 being handled 1 time
€1.11   €11.00 - UNHANDLED will result in €1.10 being handled 1 time
€112    €12.00 - UNHANDLED will result in €11 being handled 2 times
etc.

所以我正在努力的是,我正在寻找一种方法让€1.011 €11.00分成€1.0 | 11 | €11.00. 我考虑采用后者的价格,并试图找到第一个欧元金额组合之间的正确切割,使得firstEuro *金额导致第二个金额。但是,我想知道Regex中是否有一些隐藏(至少对我而言)的功能来处理这样的事情。

我很想听到一些建议。

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

        {
            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();

答案 1 :(得分:0)

最终,我走上了抓住价格/金额连续的道路并一直努力,直到找到合适的切割。对我来说:

private Product getProduct(string oneProductText)
    {
        Product product = new Product();
        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 into pieces.

        foreach (Match match in matches)
        {
            product = new Product();
            product.name = match.Groups[1].Value;
            decimal totalPrice = Decimal.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture);
            decimal[] priceAndAmount = getPricePerAndAmount(match.Groups[2].Value, totalPrice);
            product.price = priceAndAmount[0];
            product.amount = (int)priceAndAmount[1];
        }
        return product;
    }
private decimal[] getPricePerAndAmount(string priceAmount, decimal totalPrice)
    {
        decimal[] priceAndAmount = new decimal[2];
        for (int i = 1; i <= priceAmount.Length - 1; i++)
        {
            decimal amount = Decimal.Parse(Right(priceAmount, i), CultureInfo.InvariantCulture);
            decimal price = Decimal.Parse(priceAmount.Substring(0, priceAmount.Length - i), CultureInfo.InvariantCulture);
            decimal tempPrice = amount * price;

            if (totalPrice == tempPrice)
            {
                priceAndAmount[0] = price;
                priceAndAmount[1] = amount;
                break;
            }
        }
        return priceAndAmount;
    }