I would like to know the regular expression for c# fulfill the following pattern:
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);
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.
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.
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.
C# provides a class termed as Regex which can be found in System. Text. RegularExpression namespace.
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 stringI 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
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:
000123
which is in fact 123
), change each [0-9]{1,3}
fragment into 0*[0-9]{1,3}
012
must fail, when 12
or 0
must) pass, change each [0-9]{1,3}
fragment into (?:0|[1-9][0-9]{0,2})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With