正则表达式验证百分比

编程入门 行业动态 更新时间:2024-10-24 04:35:02
本文介绍了正则表达式验证百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想验证输入字符串,这样

5.0是正确的 5.5%是正确的

于是我开始用下面的代码:

字符串decimalstring1 =10000.55; 串decimalstring2 =5%5%; 字符串表达式= @^ \d | \d%; 正则表达式objNotNumberPattern =新的正则表达式(表达); Console.WriteLine(objNotNumberPattern.IsMatch(decimalstring1)); Console.WriteLine(objNotNumberPattern.IsMatch(decimalstring2)); 到Console.ReadLine();

但问题是,像5%5%输入它给正确

我如何修改这个表达式来完成这项工作?

解决方案

的String []输入=新的String [] {1000.55,1000.65%,100 100%,1400%,5.5,5.5%,×, %}; 字符串表达式= @[]?^ \d + \d *%$; 正则表达式objNotNumberPattern =新的正则表达式(表达); 的foreach(在输入VAR项) Console.WriteLine(objNotNumberPattern.IsMatch(项目));

更新

字符串表达式= @^(\d + | \d + \d +。])%$?;

I want to validate input string such that

5.0 is correct 5.5% is correct

So I started with the following code:

string decimalstring1 = "10000.55"; string decimalstring2 = "5%5%"; string expression = @"^\d|\d%"; Regex objNotNumberPattern = new Regex(expression); Console.WriteLine(objNotNumberPattern.IsMatch(decimalstring1)); Console.WriteLine(objNotNumberPattern.IsMatch(decimalstring2)); Console.ReadLine();

But the problem is that with input like 5%5% it gives correct

How can I modify this expression to make this work?

解决方案

string[] inputs = new string[] { "1000.55", "1000.65%", "100", "100%", "1400%", "5.5", "5.5%", "x", ".%" }; string expression = @"^\d+[.]?\d*%?$"; Regex objNotNumberPattern = new Regex(expression); foreach (var item in inputs) Console.WriteLine(objNotNumberPattern.IsMatch(item));

UPDATE

string expression = @"^(\d+|\d+[.]\d+)%?$";

更多推荐

正则表达式验证百分比

本文发布于:2023-11-06 06:29:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百分比   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!