I wrote a bunch of regular expressions for a C# (3.5) ASP.NET web application. I am not an advanced regular expression user by any means. I am troubleshooting one that has me totally confused to as why it is not working. Especially since I have similar ones that are working fine.
Here are the details. Thank you for your time and kind assistance.
What my application does is takes 2 individual values. If the 2 values together match a predefined set of rules, then they get mapped to a certain value.
I have defined some of the rules as such (note: these are just made up by me and have no programming significance or meaning):
Element 1: Cxxxx*####
Element 2: Czzzz*####
For Element 1:
What this means is that this sequence has to start with “C”. The next 4 characters (the four X’s) are alphanumeric. The asterisk means it can be (“R” or “D”). The # symbols mean it can be a sequence of any 4 alphanumeric characters.
For Element 2:
What this means is that this sequence has to start with “C”. The next 4 characters (the four Z’s) are alphanumeric. The asterisk means it can be (“R” or “D”). The # symbols mean it can be a sequence of any 4 alphanumeric characters.
Between the two elements, the X’s and the Z’s simply mean that the 4 character sequence cannot be the same. It must be unique between the two.
So these two elements violate the rule since the “1491” repeats in the same spot in the sequence.
Element 1: C1491D1234
Element 2: C1491D5678
This one would return true as it should be fine given the rules of the sequence:
Element 1: C1491D1234
Element 2: C1599D5678
This one would return true as it should be fine given the rules of the sequence:
Element 1: C1491D1234
Element 2: C1599D1234
This is the regular expression I am using to create this rule:
C([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4}-C(?!\1)[DdRr][A-Za-z0-9]{4}
These are the two elements I am using to test the condition:
Element 1: C1491D1491
Element 2: C1000R4100
For some reason, my regular expression is wrong and is not returning true.
This is how my C# program sees the sequence: C1491D1491-C1000R4100
I have been using this website to test:
Your regex has a subtle flaw. The
(?!\1)is a negative lookahead: It checks that the first backreference does not repeat, but it does not consume the new sequence. You can fix your code like this: