I am trying to capture a money amount using Regex using named groups.
so I have the following code:
string line1 = "Total $19 | Subtotal $0.75";
Regex r = new Regex(@"Total \$(?<total>\d+\.?\d{0,2}?) \| Subtotal \$(?<sub>\d+\.?\d{0,2}?)");
Match m = r.Match(line1);
Assert.IsTrue(m.Success);
Assert.AreEqual(19, Convert.ToDecimal(m.Groups["total"].Value));
Assert.AreEqual(0.75, Convert.ToDecimal(m.Groups["sub"].Value));
Now the regex matches the string and Success is true.
the ["total"] comes out to 19, but the ["sub"] is 0 instead of 0.75
Can someone give me an idea how to make the decimal part optional, but also have it pick it up in the group.
I have been having trouble matching the numbers, I dont know yet if I may have to squeeze a comma in there oO