8 Comments

  • Here's one that matches numbers that are divisible by 10 (assumes 0 is divisible by 10).



    \d*0



    ;)

  • I forgot to mention, watching the regex engine step through the matches can be quite educational, using the regex debugger:



    (simplified the test by removing useless cases and just using (?:...)* )



    perl -Mre=debug -wle'print "Divisible by 3\n" if ("x"x shift)=~/^(?:...)*$/;' 9



    Contrast the "Non-prime"



    perl -Mre=debug -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/' 9



    with the "prime"



    perl -Mre=debug -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/' 7



    The prime case takes a LOT of operations to prove, relatively speaking (almost twice as many). So regex "computers" may not be the efficiency wave of the future :)

  • If a=[0369] and b=[147] and c=[258],

    then (a|b(a|ba*c)*(c|ba*b)|c(a|ca*b)*(b|ca*c))*

    is the regular expression for determining if a number is divisible by 3

  • I`m having problems with the programming syntax of RegExs, but basicly, I think you`re close; my solution was:



    A = (0+3+6+9)*

    B = (1+4+7)

    C = (2+5+8)



    r = ABABABA + ABACA + ACABA + ACACACA + ACACABABA + A



    Or in other words:



    r = A ( BA(BAB+C) + CA( B + CA(C+BAB) ) )* A



    I can`t, for the life of me, write that in the syntax you used, so I guess that`s the end of my contribution to this thread.





    Ariel



  • If a=[0369] and b=[147] and c=[258],

    then (a|b(a|ba*c)*(c|bb)|c(a|ca*b)*(b|cc))+ is a number divisible by 3.

  • Sorry, I answer the question twice. Anyway, the first answer is correct except that it also excepts the empty string. The second answer is missing a couple of a's and should be (a|b(a|ba*c)*(c|ba*b)|c(a|ca*b)*(b|ca*c))+ to match the first answer, with the exception of the plus sign at the end.



    Any Ariel's answer misses a number of the form BBCBB such as 11211, I believe.

  • "RegEx for divisibility by 7?" he asked daringly...

  • A test for seven exists, and is feasible, since you can design an automaton to do it`s job (so all you need to do is convert it to a regEx).

Comments have been disabled for this content.