views:

559

answers:

2

Is it possible to create a mod 11 check digit routine with a regex statement?

THe nubmer is a 10 digit number,

Step 1: A = (2nd number * 2) + (3rd number * 4) + (4th number * 8) + (5th number * 5) + (6th number * 10) + (7th number * 9) + (8th number * 7) + (9th number * 3))

Step 2: B = A / 11 (ignor remainder)

Step 3: C = B * 11

Step 4: D = A - C

Step 5: 11 - D must = the 10th digit

+3  A: 

No - fundamentally you're wanting to do maths here, and that doesn't really fit with regular expressions which are just about patterns.

I mean, theoretically it's certainly possible - you could list all valid numbers, and combine them into one enormous regex. However, it's not practically feasible.

Jon Skeet
Exactly what I was about to say. Regex isn't a turing-complete language; math is one of the things it cannot do.
ojrac
A: 

Regex does pattern matching, not really parsing. You need to parse the number to get the digits. I would suggest using the regex to ensure it is a 10 digit number and then ToString() it and chop it up.

Craig