Given a decimal number N
as a string of digits, how do I check if it\'s divisible by M
using regular expressions only, without con
If you are allowed to modify the string and repeat, you can do one step of long division at a time. sed syntax for 7: reapply until you get the remainder. Stop when you have a single 0, 1, 2, 3, 4, 5, or 6.
s/^0//
s/^7/0/
s/^8/1/
s/^9/2/
s/^([18]4|[29][18]|35|4[29]|56|63)/0/
s/^([18]5|[29][29]|36|43|5[07]|64)/1/
s/^([18]6|[29]3|3[07]|44|5[18]|65)/2/
s/^([18][07]|[29]4|3[18]|45|5[29]|66)/3/
s/^([18][18]|[29]5|3[29]|46|53|6[07])/4/
s/^([18][29]|[29]6|33|4[07]|54|6[18])/5/
s/^([18]3|[29][07]|34|4[18]|55|6[29])/6/
But it's very weak sauce. Easier to do away with the "regex" altogether and just read in a character at a time and switch to the appropriate state. If that's not an option, then I suspect you are out of luck for 7 and 13, although 11 might still be possible.