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"


Need Your Help

Using C# how to clean up MSMQ message format to work with C++ IXMLDOMDocument2

c# c++ character-encoding msmq

I'm trying to get a C++ service to load an XML document from a MSMQ message generated by C#. I can't really change the C++ side of things because I'm trying to inject test messages into the queue. ...

starting minidfscluster in tests

java testing hadoop junit classloader

I'm starting MiniDfsCluster (my dependencies are 2.0.0-cdh4.5.0) in my test. I use a simple routine to start it:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.