Priyank Bhavsar
Priyank Bhavsar

Reputation: 51

Regular expression not working in c#

I want to match the following 2 patterns.

The regular expression I'm using is ^[0-9]{5}|[0-9]{5}.[0-9]{5}$. It is working for the first pattern but not the second. I have tested with Regex tester for .net and it is working. Can someone let me know what is wrong with my regex?

Upvotes: 1

Views: 828

Answers (3)

Brett Allen
Brett Allen

Reputation: 5507

Try ^([0-9]{5}|[0-9]{5}\.[0-9]{5})$

Upvotes: 1

Pablo Expósito
Pablo Expósito

Reputation: 162

Maybe you should use.

([0-9]{5})(\.[0-9]{5})*

You can test yours own regex expression in: https://regexr.com/

EDIT:

([0-9]{5})(\.[0-9]{5})|([0-9]{5})

Upvotes: -1

user8502296
user8502296

Reputation:

I tested it and that did not actually work for me. You should specify that '.' is a literal.

This works:

([0-9]{5})(\.[0-9]{5})?

Upvotes: 1

Related Questions