c#正则表达式与正则表达式测试工具结果不同

ijunxiong 2017-02-26 04:30:08
System.Text.RegularExpressions.Regex.Match("-12323.123232","(0|[1-9][0-9]*)").Groups[0].Value
"12323"
System.Text.RegularExpressions.Regex.Match("12323.123232","(0|[1-9][0-9]*)").Groups[0].Value
"12323"
//以上正解
System.Text.RegularExpressions.Regex.Match("12323.123232","([0-9]*(\\.[0-9]{0,4})?)").Groups[0].Value
"12323.1232"
System.Text.RegularExpressions.Regex.Match("-12323.123232","([0-9]*(\\.[0-9]{0,4})?)").Groups[0].Value
"" //这个,怎么不是12323.1232???


用在线的js正则表达式测试 // tool .oschina .net/ regex/
-12323.123232   匹配:([0-9]*(\.[0-9]{0,4})?),得到12323.1232
...全文
171 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ijunxiong 2017-02-26
  • 打赏
  • 举报
回复
终于搞定了,、、这样小数都可以是Matches[0]获取,和Group[1]匹配到 System.Text.RegularExpressions.Regex.Matches("-12.","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value "-12." System.Text.RegularExpressions.Regex.Matches("-12.23","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value "-12.23" System.Text.RegularExpressions.Regex.Matches("-.12","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value "-.12" System.Text.RegularExpressions.Regex.Matches("12.","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value "12." System.Text.RegularExpressions.Regex.Matches("12.23","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value "12.23" System.Text.RegularExpressions.Regex.Matches(".23","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value ".23" System.Text.RegularExpressions.Regex.Matches("+12.","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value "+12." System.Text.RegularExpressions.Regex.Matches("+12.23","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value "+12.23" System.Text.RegularExpressions.Regex.Matches("+.23","([-+]?[0-9]+(\\.[0-9]{0,4})?|[-+]?[0-9]*\\.[0-9]{1,4})")[0].Groups[1].Value "+.23"
ijunxiong 2017-02-26
  • 打赏
  • 举报
回复
@Forty2 哦,谢谢,我再测测
Forty2 2017-02-26
  • 打赏
  • 举报
回复
是"4 处匹配" 不是匹配的”4 个组“
Forty2 2017-02-26
  • 打赏
  • 举报
回复
System.Text.RegularExpressions.Regex.Matches(...
ijunxiong 2017-02-26
  • 打赏
  • 举报
回复
@Forty2 1。我测的结果不一样,net中([0-9]*(\.[0-9]{0,4})?)得到的Match.Groups有3个,都是空串,。。不明白你说的一样,是说([0-9]*(\.[0-9]{0,4})?)可以得到12323.1232? 2。([0-9]+(\.[0-9]{0,4})?)这个我测的,可是如果我输入.1234,它匹配的是1234,自动没有小数点了,我需要.1234这样也能用
Forty2 2017-02-26
  • 打赏
  • 举报
回复
其实oschina给出的结果,和dotnet的结果一样::
引用
共找到 4 处匹配: (空行) 12323.1232 32 (空行)
因为你的表达式,也可以匹配空字符串。 如果你的原意是匹配数字,你可以改成:([0-9]+(\.[0-9]{0,4})?)

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧