这不算分! [英] This doesn't make cents!

查看:111
本文介绍了这不算分!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很简单。 我只想验证并解析一个简单的美元和美分价值。 所以我想出了......


(?< dollars> \d {0,6})(\。(?< cents> \d {0, 3}))?


...它匹配它应匹配的东西但也给我(使用Expresso)假的NULL匹配。


Expresso屏幕拍摄下面,但如果它不太清晰,则"样本文本"我正在使用的是...


7.34

.45

9 zh
11.345
6.

.5


NULL匹配来自哪里? 如果我改变表达式使得需要1到6美元的数字,则NULL匹配消失,但是然后".45"。匹配为45美元。 这是Express屏幕截图。 我将不胜感激任何
帮助(或同情)。


谢谢,Bob



解决方案

< blockquote>

试试这段代码。这是通用代码,这意味着它将匹配美元部分和分数部分中的任意数字。

 Sub Main()

Dim lstDollars =新列表(字符串)来自{
" 7.34",
" .45",
" ; 9",
" 11.345",
" 6.",
" .5"
}

Dim re = New Regex(" ^(?< dollar> \d +)?(\。?)(?< cent> \d +)?


")

For each dollar in lstDollars
Dim m = re.Match(dollar)
if m.Success Then Console .WriteLine(" Dollar:'{0}'::: Cent:'{1}'",m.Groups(" dollar")。value,m.Groups(" cent"。)。Value)
下一个


Console.WriteLine()
Console.Write(按任意键退出...")
Console.ReadKey( )

End Sub


I thought this would be simple.  I just want to validate and parse a simple dollar and cents value.  So I came up with ...

(?<dollars>\d{0,6})(\.(?<cents>\d{0,3}))?

... which matches what it should match but also gives me (using Expresso) bogus NULL matches.

Expresso screen shot below, but if it's not too legible the "Sample Text" I am using is ...

7.34
.45
9
11.345
6.
.5

Where are the NULL matches coming from?  If I change the expression so that between 1 and 6 dollar digits are required the NULL matches go away, but then ".45" matches as 45 dollars.  Here's the Express screen shot.  I'll be grateful for any help (or sympathy).

Thanks, Bob

解决方案

Try this code. This is general code which means it will match ANY number of digits both in dollar part and cent part.

Sub Main()

        Dim lstDollars = New List(Of String) From {
           "7.34",
           ".45",
           "9",
           "11.345",
           "6.",
           ".5"
       }

        Dim re = New Regex("^(?<dollar>\d+)?(\.?)(?<cent>\d+)?


") For Each dollar In lstDollars Dim m = re.Match(dollar) If m.Success Then Console.WriteLine("Dollar: '{0}' ::: Cent: '{1}'", m.Groups("dollar").Value, m.Groups("cent").Value) Next Console.WriteLine() Console.Write("Press any key to exit...") Console.ReadKey() End Sub


这篇关于这不算分!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆