繁体   English   中英

正则表达式,允许数值在[1-100]和百分比符号之间

[英]Regex to allow numeric values between[1-100] and a percentage symbol

我希望正则表达式允许1到100之间的数值和一个百分比符号

有效样本:0%,100%,100、0、65%

无效样本:101、101%,100.1、65.6%,65.6

任何帮助表示赞赏

我不会讲正则表达式,但是您说任何帮助将不胜感激,所以我在这里:

    bool Verify(string input)
    {
        input = input.Replace("%", "");  // if it contains % remove it
        int value;
        if (Int32.TryParse(input, out value))   //if the input can be converted into a number
        {
            if (value > 1 && value < 100)  //and the value is in range
            {
                return true;  //return true to confirm it
            }
        }
        return false;  //in any other case return false
    }

尝试这个:

^(0*100{1,1}\.?((?<=\.)0*)?%$)|(^0*[1-9]\d{0,1}\.?((?<=\.)\d*)?%)$

它将允许65%100%并丢弃65100.1%

匹配0到100(含)之间的百分比。 最多接受2个小数位。 例如:0%,100%、. 17%

  ^( 100(?:\.0{1,2})? | 0*?\.\d{1,2} | \d{1,2}(?:\.\d{1,2})? )% $

暂无
暂无

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

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