简体   繁体   English

字符串C#中的正则表达式匹配数

[英]Regex match number in string C#

I am trying to get the total price from a receipt with Regex. 我正在尝试从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 . 我以TOTAL[ .0-9]开始,但这仅返回单词TOTAL

I googled around and putted this one together but can't get it to work: 我在Google上四处搜寻,并将其放在一起,但无法正常工作:

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: 我试图从这样格式化的HTML文件中获取它:

TOTAL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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: 但是,正如您在注释中指出的那样,它来自HTML,并且包含没有换行符的字符实体,因此您也需要考虑以下内容:

TOTAL(\\s+|(&nbsp;)+)(?<value>[.0-9]+)

您的正则表达式可以正常工作(按照建议检查您的输入),但是它有一个小错误:它将捕获数字和点的任何组合(例如333.3.2.22 ....),更好的方法是:

TOTAL\s+(?<value>\d+\.\d+)

(?(\\b.*\\b\\s)([0-9.]*[0-9])) should work. (?(\\b.*\\b\\s)([0-9.]*[0-9]))应该可以工作。

I would recommend you to use the Regex hero online editor which is at least really helpful for me. 我建议您使用Regex hero在线编辑器 ,这至少对我真的很有帮助。

If you have only a single whitespace between TOTAL and the amount you can use a whitespace in the regex. 如果在TOTAL和数量之间只有一个空格,则可以在正则表达式中使用空格。 Additionally, try this: 此外,请尝试以下操作:

sRegex = "TOTAL ([0-9]+\.[0-9]+)";

See here for the MSDN reference. 有关MSDN参考,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM