【问题标题】:regex to allow only single digit or two digit numbers and comma in C#正则表达式仅允许 C# 中的一位数或两位数和逗号
【发布时间】:2021-07-30 01:14:18
【问题描述】:

我搜索了互联网,甚至遵循了 RegEx 的文档,但无法解决问题。 我想验证一位数或两位数。 它应该只接受

用例 1:字符串 sDate= ",01,23,05,1,28,25";

用例 1:字符串 sDate= ",01,23,05,1,28,25,";

用例 1:字符串 sDate= "01,23,05,1,28,25,";

用例 1:字符串 sDate="01,23,05,1,28,25";

它不应该接受任何东西,即没有小数、字符、除逗号以外的特殊字符。 我用 C# 写了一段代码

string stDate=",01,23,05,1,28,25";

if (!string.IsNullOrEmpty(sDate))

            {
                Regex r = new Regex(@"^(\d+[,]\d*|\d*[,]\d+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase); 
                if (r.IsMatch(sDate))
                {
business logic
}
}
else
{
//business logic
}

我需要知道我哪里出错了。当我在字符串中输入一个字母时,它甚至会接受它。 任何帮助将不胜感激。 在此处输入代码 在此输入代码

【问题讨论】:

  • 如果您验证整个输入,请使用^,?\d+(?:,\d+)*,?$
  • 仅用于验证 - @WiktorStribiżew's 的更改 - ^,?\d{1,2}(?:,\d{1,2})*,?$ 也获得匹配 - ^,?(\d{1,2})(?:,(\d{1,2}))*,?$ 在这里测试它们:regexstorm.net/tester

标签: c# regex


【解决方案1】:

要匹配可选的 1 位或 2 位数字重复,其中逗号可以位于开头、结尾或中间,您可以使用:

^,?\d\d?(?:,\d\d?)*,?$
  • ^ 字符串开始
  • ,?\d\d? 匹配可选逗号、数字和可选数字
  • (?:,\d\d?)* 匹配逗号、数字和可选数字的 0 次或多次重复
  • ,? 匹配可选逗号
  • $字符串结束

查看regex demo

【讨论】:

    【解决方案2】:

    看起来new Regex(@"^(,\d{1,2})+$") 适合你。

    ^(\d+[,]\d*|\d*[,]\d+)$ 的正则表达式开始时需要一个或多个数字到 \d+,后跟一个强制逗号,然后是任意数量的数字到 \d* 或任意数量的数字。

    this 之类的网站非常适合测试和练习正则表达式。该站点实际测试了php,但没有显着差异。

    【讨论】:

      【解决方案3】:

      使用

      Regex r = new Regex(@"\A,?[0-9]+(?:,[0-9]+)*,?\z", RegexOptions.Compiled);
      

      regex proof

      相关帖子

      解释

      --------------------------------------------------------------------------------
        \A                       the beginning of the string
      --------------------------------------------------------------------------------
        ,?                       ',' (optional (matching the most amount
                                 possible))
      --------------------------------------------------------------------------------
        [0-9]+                   any character of: '0' to '9' (1 or more
                                 times (matching the most amount possible))
      --------------------------------------------------------------------------------
        (?:                      group, but do not capture (0 or more times
                                 (matching the most amount possible)):
      --------------------------------------------------------------------------------
          ,                        ','
      --------------------------------------------------------------------------------
          [0-9]+                   any character of: '0' to '9' (1 or more
                                   times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
        )*                       end of grouping
      --------------------------------------------------------------------------------
        ,?                       ',' (optional (matching the most amount
                                 possible))
      --------------------------------------------------------------------------------
        \z                       the end of the string
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多