6. .5 NULL匹配来自哪里? 如果我改变表达式使得需要1到6美元的数字,则NULL匹配消失,但是然后".45"。匹配为45美元。 这是Express屏幕截图。 我将不胜感激任何帮助(或同情)。 谢谢,Bob 解决方案 试试这段代码。这是通用代码,这意味着它将匹配美元部分和分数部分中的任意数字。 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.45911.3456..5Where 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 这篇关于这不算分!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS! 05-21 13:47