I am trying to get the total price from a receipt with Regex.
The formatting is:
TOTAL 15.40
The goal is only to get the price out of the string.
I started with TOTAL[ .0-9]
, but this only returned the word TOTAL
.
I googled around and putted this one together but can't get it to work:
TOTAL(\\s+)(?<value>[.0-9]+)
I have made the following code:
sRegex = "TOTAL(\\s+)(?<value>[.0-9]+)";
Match match = Regex.Match(this.sHTMLResult, sRegex, RegexOptions.None);
if (match.Success)
Console.Out.WriteLine("regex good");
else
Console.Out.WriteLine("regex fail");
But the regex doesn't return a success.
I try to get it out of a HTML file formatted like this:
TOTAL 15.40
您可以使用:
"TOTAL *(\\d*.\\d*)"
Your initial regular expression works fine with the supplied text:
TOTAL(\\s+)(?<value>[.0-9]+)
However, as you indicated in comments, this is from HTML and contains the character entities for no break spaces, so you need to account for those as well:
TOTAL(\\s+|( )+)(?<value>[.0-9]+)
您的正则表达式可以正常工作(按照建议检查您的输入),但是它有一个小错误:它将捕获数字和点的任何组合(例如333.3.2.22 ....),更好的方法是:
TOTAL\s+(?<value>\d+\.\d+)
(?(\\b.*\\b\\s)([0-9.]*[0-9]))
should work.
I would recommend you to use the Regex hero online editor which is at least really helpful for me.
If you have only a single whitespace between TOTAL and the amount you can use a whitespace in the regex. Additionally, try this:
sRegex = "TOTAL ([0-9]+\.[0-9]+)";
See here for the MSDN reference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.