-1

For a study I am doing everyone gets assigned a unique number. All unique numbers are divisble by 11 (this is done because it makes sequential numbers quite different from each other).

I would ideally like a regex which I can use to check that the number entered in the study_id field is an acceptable value - e.g divisible by 11. I will have leading zeroes to a maximum of 5 digits So:

  • 00011 - Acceptable
  • 00012 - Not Acceptable
  • 13211 - Acceptable
  • 13221 - Not Acceptable

Any suggestions gratefully received

mmarks
  • 1,119
  • 7
  • 14
  • I would be surprised if this was possible to do using a regular expression. – Adrian Jun 06 '16 at 16:05
  • 1
    Similar question, interesting reading: [How do I write a regular expression that matches an IPv4 dotted address?](https://blogs.msdn.microsoft.com/oldnewthing/20060522-08/?p=31113) – Álvaro González Jun 06 '16 at 16:41
  • 1
    It could be possible as there even is a shorthand rule for calculating division with 11, but considering how long the regex for divisible by 3 is, I'd say this is way too broad. – Antti Haapala -- Слава Україні Jun 07 '16 at 05:16
  • 3
    Possible duplicate of [Check number divisibility with regular expressions](http://stackoverflow.com/questions/12403842/check-number-divisibility-with-regular-expressions) – Zero Piraeus Jun 07 '16 at 05:37

1 Answers1

3

This isn't possible because there are no textual similarities between numbers that are divisible by 11. Regex is used for text matching.

For example 000165 is divisible by 11 as is 00011.

The best way to validate the number is to divide it by 11 and see if there is any remainder. So in Excel you'd do this:

=IF(MOD(165, 11) = 0, "VALID", "INVALID")

Or C# you'd do something like this

bool isValid = 165 % 11 == 0;

(Disclaimer I'm not familiar with ODK so I can't provide a suitable sample; I've just guessed on the best language to write in)

Andrew Skirrow
  • 3,402
  • 18
  • 41
  • This is not true. The set of numbers divisible by eleven (the same is true for any other natural number, obviously) does form a regular language, therefore a regular expression for the language exists. For instance `^((0*1(10*1)*0(0(10*1)*0|1)*0(10*1)*10*)|(0*(1(10*1)*10*)?))$` is a RE testing divisibility of binary numbers by 3. Theoretically (and fairly straightforwardly), you could construct such a RE for any divisibility checking. However, practically, the RE would be _huge_. So, yeah, you don't want to do that, you want to use standard arithmetic instead. – Mormegil Apr 21 '23 at 09:44