Need to write a regex in C# for numbers only
I need a regex in C# for the following conditions which will be validating a textbox entry :
I should contain either A 5-digit number or a 6-digit number. In case of multiple entries, the numbers should be separated by a pipe character without space. Example: 34786|235652|12876
I have tried the following regex , which are not working propyl:
^\d{5,6}\|?\d{5,6}?$ ^[\d{5,6}+][\|?][\d{5,6}?]$ (^\d{5,6}$)|(^\d{5,6}\|?[\d{5,6}*]$)
Please help!!
Answers
Try this:
^\d{5,6}(\|\d{5,6})*$
This should work:
^[0-9]{5,6}(\|[0-9]{5,6})*$
Explaination:
^ = Start of line
[0-9] = Any digit (\d would work too)
{5,6} = Either 5 or 6 times
(...)* = Whatever is inside the (), 0 or more times
\| = The pipe character
$ = End of line
Put together, it's "Start of line followed by 5 or 6 digits followed by [pipe character followed by 5 or 6 digits] 0 or more times followed by end of line"