简体   繁体   English

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

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

I want a regex to allow numeric values between 1 to 100 and one percentage symbol 我希望正则表达式允许1到100之间的数值和一个百分比符号

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

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

Any help is appreciated 任何帮助表示赞赏

i dont speak regex but you saied any help would be appreciated so here i go: 我不会讲正则表达式,但是您说任何帮助将不胜感激,所以我在这里:

    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
    }

Try this: 尝试这个:

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

It will allow 65% and 100% and discard 65 and 100.1% 它将允许65%100%并丢弃65100.1%

Matches a percentage between 0 and 100 (inclusive). 匹配0到100(含)之间的百分比。 Accepts up to 2 decimal places. 最多接受2个小数位。 ex: 0% , 100% , .17% 例如: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