简体   繁体   中英

regex to allow only single digit or two digit numbers and comma in C#

I searched internet and even followed the documentation of RegEx but unable to solve the issue. I want to validate single digit or double digit number. It should only accept

Use Case1: string sDate= ",01,23,05,1,28,25";

Use Case1: string sDate= ",01,23,05,1,28,25,";

Use Case1: string sDate= "01,23,05,1,28,25,";

Use Case1: string sDate= "01,23,05,1,28,25";

It should not accept anything ie no decimal, characters, special character except comma. I have written a code in 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
}

I need to know where I"m going wrong. When I'm entering a alphabet in the string it is accepting it even. Any help would be highly appreciated. enter code here enter code here

To match optional repetitions of 1 or 2 digits where the comma can be at the start, end or in between, you can use:

^,?\d\d?(?:,\d\d?)*,?$
  • ^ Start of string
  • ,?\d\d? Match optional comma, a digit and optional digit
  • (?:,\d\d?)* Match 0 or more repetitions of a comma, digit and optional digit
  • ,? Match optional comma
  • $ End of string

See a regex demo

Looks like new Regex(@"^(,\d{1,2})+$") would work for you.

Your regex of ^(\d+[,]\d*|\d*[,]\d+)$ starts out by expecting one or more digits through \d+ followed by a mandatory comma, followed by any number of digits through \d* OR any number of digits again.

Sites like this are great for testing and practicing regexes. The site actually tests php but there is no significant difference.

Use

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

See regex proof .

Relevant posts :

EXPLANATION

--------------------------------------------------------------------------------
  \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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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