Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to write a regex in C# for numbers only

Tags:

c#

regex

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

like image 400
Iti1000 Avatar asked Feb 15 '23 11:02

Iti1000


2 Answers

Try this:

^\d{5,6}(\|\d{5,6})*$
like image 195
kol Avatar answered Feb 23 '23 20:02

kol


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"

like image 36
Brian Kintz Avatar answered Feb 23 '23 18:02

Brian Kintz