John
John

Reputation: 217

regular expression with or condition in middle of expression

I'm having a problem with an | expression in the middle of my regular expression validator in asp.net. I want a user to be able to enter a string like this

AB/123/123/12
AB/12/123/12

Here is my regular expression

        AB[\/]^\d{3}|\d{2}$[\/]\d{3}[\/]\d{2}

I've tested this part seperately

       ^\d{3}|\d{2}$ 

and it works but when it's in the middle of the first regular expression it doesn't seem to. It's probably something small i'm missing but I can't see it. Thanks in advance for any help

Upvotes: 0

Views: 1733

Answers (2)

Anonymous
Anonymous

Reputation: 12017

Putting the starting and ending anchors (^ and $) anywhere other than a position where they could start or end the string means that the regular expression is bound to fail to match. Instead, you could use parentheses to confine what the or statement is relevant to:

AB[\/](\d{3}|\d{2})[\/]\d{3}[\/]\d{2}

But, it's better to just condense it as far as possible.

AB\/\d{2,3}\/\d{3}\/\d{2}

Upvotes: 3

Matthew Wells
Matthew Wells

Reputation: 104

How about ^AB\/(\d{3}|\d{2})\/\d{3}\/\d{2}$

Upvotes: 0

Related Questions