Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从连接字符串上的金额拼接价格_C#_Regex - Fatal编程技术网

C# 从连接字符串上的金额拼接价格

C# 从连接字符串上的金额拼接价格,c#,regex,C#,Regex,我在做POS系统。此版本将替换当前使用的现有版本。正在使用的版本是用Visual Basic编程的,有大量现已过时和未使用的类和屏幕,而这个新版本将使用Visual C#(WinForms)。我正在寻找一种方法将收据放入一个列表中(其中产品包含名称、价格和金额)。但是,旧收据存储在单个数据库单元中,如下所示: Title of product A€ 449,001€ 449,00Title of product B€

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

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}”将它们分割成一个产品的单个字符串↨")和之后的切片(可能一次就可以完成,但我发现这一点更清楚)

这将产生一个字符串列表,其中包含[标题、价格/每项、金额和该项的总价]:

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
然后我使用(假设产品已声明)

这将适用于大多数字符串到产品的转换,但也有一些特殊情况没有得到处理。我正在寻找一种方法来处理这些情况。 包含以下内容的字符串:

€.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.00欧元。
我考虑采用后一种价格,并试图在第一个欧元金额组合之间找到正确的切入点,这样第一个欧元*金额会产生第二个金额。然而,我想知道Regex中是否有一些隐藏的(至少对我来说)功能来处理这样的事情

我很想听到一些建议。

尝试以下内容:

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

最终,我选择了抓住价格/金额,并尝试着找到合适的价格。对我来说:

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;
    }
私有产品getProduct(字符串oneProductText)
{
产品=新产品();
MatchCollection matches=Regex.matches(oneProductText,@”(.*)€(\d*?\d{0,2}\s*[1-9]+\d*)\s*€(\d*\d{0,2})↨");
//文本-价格/每金额-价格总额| |破碎后使用。
foreach(匹配中的匹配)
{
产品=新产品();
product.name=match.Groups[1]。值;
decimal totalPrice=decimal.Parse(match.Groups[3].Value,CultureInfo.InvariantCulture);
decimal[]priceAndAmount=getPricePerAndAmount(match.Groups[2]。值,totalPrice);
product.price=priceAndAmount[0];
product.amount=(int)价格和金额[1];
}
退货产品;
}
私有十进制[]getPricePerAndAmount(字符串priceAmount,十进制totalPrice)
{
十进制[]价格和装载量=新的十进制[2];

对于(int i=1;我已经考虑过通过
HtmlDecode
运行它来将编码的欧元符号更改为实际字符?然后您应该可以很容易地在
分隔符上拆分它。它看起来可能需要多次传递。实际上,更仔细地查看示例数据,我不确定它应该如何e分隔符。字段之间似乎没有任何分隔符。@BradleyUffner我想我在这里有一个错误。我已经把
&;euro;
改成了一个欧元符号。我现在正在研究如何获得每件产品的价格和购买的产品数量。为了清楚起见,我将编辑我的问题。如何您想处理
€1.011
可能是
€1.0 | 11
€1.01 | 1
中的歧义吗?另外,追踪提出该数据格式的人,并给他们一个很好的有力打击。
        {
            string input = "Title of product A&amp;euro; 449,001&amp;euro; 449,00Title of product B&amp;euro; 14.2&amp;euro; 28.00Title of product C&amp;euro; €0.52 €1Title of product D&amp;euro; €220 €40";
            string pattern = "(?'title'Title of product [A-Z]+)&amp;(?'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();
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;
    }