I have an ASP.NET application written in C# (3.5 framework) where users have provided me with a list of patterns that we have put rules around. I could write code to manually account for each pattern, but would prefer to find a way to use regular expressions (or any method really) to handle them so I can allow the users to create more patterns (following some guidelines of course) in the future.
I will do my best to show exactly what it is I am trying to do. I really appreciate your help.
Here are a few of the patterns:
Pattern 1:
Element 1: CxxxxRxxxx
Element 2: CxxxxRzzzz
Result: Bucket 1
Pattern 2:
Element 1: CxxxxRxxxx
Element 2: CxxxxRxxxx
Result: Bucket 2
Pattern 3:
Element 1: PCxxxxxxxx
Element 2: PCzzzzzzzz
Result: Bucket 3
Pattern 4:
Element 1: PCxxxxxxxx
Element 2: UxxxxRxxxx
Result: Bucket 4
Here’s what they mean and how I need to handle them. For starters, all the elements are always 10 characters in length. The alphabetical characters are constant for each pattern. The X’s and Z’s can be numbers or letters. What the X’s and Z’s signify in the pattern is whether the other parts of the 2 elements match each other.
For instance, my input data is “C1234R5678” as element 1 and “C1234R9999” for element 2. In this case, the result would be “Bucket 1” since this matches the pattern defined for this condition (the numbers after the “C” match, but the numbers after the “R” do not).
In another example, my input data is “C1234R5678” as element 1 and “C1234R5678” for element 2. In this case, the result would be “Bucket 2” since this matches the pattern defined for this condition (the numbers after the “C” match, and the numbers after the “R” match as well).
In another example, my input data is “PC12345678” as element 1 and “PC87654321” for element 2. In this case, the result would be “Bucket 3” since this matches the pattern defined for this condition (the numbers after the “PC” do not match).
In a final example, my input data is “PC12345678” as element 1 and “U1234R5678” for element 2. In this case, the result would be “Bucket 4” since this matches the pattern defined for this condition (the 4 numbers after the “PC” match the 4 numbers after the U and the last 4 digits of the PC element match the 4 digits after the R of element 2).
As you can see, the patterns can be different in that sometime you are identifying 4 numbers after a letter or a whole run of numbers after a letter.
Thanks for your time.
You can do something like:
This will construct a bunch of strings like:
Then for each input data append Element1 and Element2 separated by a ‘-‘ (e.g.
"C1234R5678-C1234R9999") and match against each pattern, and stop at the first match.