Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx allowing digit, dash, comma

Tags:

c#

regex

I would like to know the regular expression for c# fulfill the following pattern:

  • only allow numbers, comma and digit
  • must start and end with digit
  • range of digit is 0 to 999
  • digits (e.g. 1,2,3) or range (e.g. 1-3, 2-5) are separated by ','
  • between two ',' must be a digit or range
  • '-' must start and end with digit
  • Allow only 0-1 white space after ','

Example:

1-100,134,200 --> PASS. Maximum range of numbers 0-999
1,18,100      --> PASS
1, 18, 100    --> PASS. Allow 0-1 white space after ',' 
1,  18,100    --> FAIL. Due to more than 1 white space after ','
1-,18,100     --> FAIL. Due to no digit after '-'
-2,18,100     --> FAIL. Due to no digit before '-'
1,,18,100     --> FAIL. Due to no digit between ','
1, ,18,100    --> FAIL. Due to no digit between ','
,2,18,100     --> FAIL. Due to no digit before ','
1,18,100,     --> FAIL. Due to no digit after ','

I tried using the following code but it always return a true result:

string pattern = @"[0-9]+(?:-[0-9]+)?(,[0-9]+(?:-[0-9]+)?)*";
string test = @"1-5,13,238,-a";
bool result = Regex.IsMatch(test, pattern);
like image 741
user1599647 Avatar asked May 17 '19 07:05

user1599647


People also ask

How do you use commas in regex?

The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set. The plus + indicates that one or more of the "previous item" must be present.

How do you match a hyphen in regex Java?

In regular expressions, the hyphen ("-") notation has special meaning; it indicates a range that would match any number from 0 to 9. As a result, you must escape the "-" character with a forward slash ("\") when matching the literal hyphens in a social security number.

How do you represent a number in regex?

A number can start with a sign (- or +) or with a digit. This can be captured with the expression [-+]? , which matches a single "-", a single "+" or nothing. A number can have zero or more digits in front of a single period (.) and it can have zero or more digits following the period.

Does C# have regex?

C# provides a class termed as Regex which can be found in System. Text. RegularExpression namespace.


Video Answer


2 Answers

You can use this regex,

^(?:[1-9]\d\d|[1-9]?\d)(?:-(?:[1-9]\d\d|[1-9]?\d))?(?:,\s?(?:[1-9]\d\d|[1-9]?\d)(?:-(?:[1-9]\d\d|[1-9]?\d))?)*$

Explanation:

  • ^ - Start of string
  • (?:[1-9]\d\d|[1-9]?\d) - Represents a number 0 to 999 and does not allow numbers with leading zeroes like 005
  • (?:-(?:[1-9]\d\d|[1-9]?\d))? - Optionally also allows a number separated by hyphen - so together with first regex digit, it supports numbers like 22 and 22-33 etc
  • (?:,\s?(?:[1-9]\d\d|[1-9]?\d)(?:-(?:[1-9]\d\d|[1-9]?\d))?)* - This part just supports comma separated optionally followed by a whitespace and whole of it zero or more times
  • $ - End of string

I could have used \d{1,3} to represent a number from 0 to 999 but this would allow numbers like 004 which doesn't seem to be allowed seeing your sample data. But if indeed it is okay to allow numbers like 004 or 04 then you can replace [1-9]\d\d|[1-9]?\d with \d{1,3} in my regex to make it simple.

Regex Demo

like image 184
Pushpesh Kumar Rajwanshi Avatar answered Oct 25 '22 06:10

Pushpesh Kumar Rajwanshi


You can try

   ^[0-9]{1,3}(?:\-[0-9]{1,3})?(?:,\s?[0-9]{1,3}(?:\-[0-9]{1,3})?)*$

pattern where

   ^                             String start
   0*[0-9]{1,3}                  1 to 3 digits
   (?:\-[0-9]{1,3})?             Possible minus (-) followed 1 to 3 digits (e.g. -456)
   ?:,\s?                        Comma and at most one whitespace  
   [0-9]{1,3}(?:\-[0-9]{1,3})?)* 1 to 3 digits or range repeated zero or more times
   $                             End of string        

Demo:

  string pattern = 
    @"^[0-9]{1,3}(?:\-[0-9]{1,3})?(?:,\s?[0-9]{1,3}(?:\-[0-9]{1,3})?)*$";

  string[] tests = new string[] {
    "123",
    "1234",
    "123-456",
    "123,456",
    "1-100,134,200",
    "1,18,100",
    "1, 18, 100",
    "1,  18,100",
    "1-,18,100",
    "-2,18,100",
    "1,,18,100",
    "1, ,18,100",
    ",2,18,100",
    "1,18,100,",
  };

  string[] results = tests
    .Select(test => $"{test,-20} --> {(Regex.IsMatch(test, pattern) ? "PASS" : "FAIL")}")
    .ToArray();

  string report = string.Join(Environment.NewLine, results);

  Console.Write(report);

Outcome:

123                  --> PASS
1234                 --> FAIL 
123-456              --> PASS
123,456              --> PASS
1-100,134,200        --> PASS
1,18,100             --> PASS
1, 18, 100           --> PASS
1,  18,100           --> FAIL
1-,18,100            --> FAIL
-2,18,100            --> FAIL
1,,18,100            --> FAIL
1, ,18,100           --> FAIL
,2,18,100            --> FAIL
1,18,100,            --> FAIL

Edit:

  • If you want to allow arbitrary many leading zeros (e.g. 000123 which is in fact 123), change each [0-9]{1,3} fragment into 0*[0-9]{1,3}
  • If you want ban leading zeroes (012 must fail, when 12 or 0 must) pass, change each [0-9]{1,3} fragment into (?:0|[1-9][0-9]{0,2})
like image 20
Dmitry Bychenko Avatar answered Oct 25 '22 06:10

Dmitry Bychenko