# Regex (Perl / PCRE), 36 bytes x(x*),((x(?=((?(4)\4)\1)))*)\4?+(x*) [Try it online!](https://tio.run/##VVFRb4IwEH7nVxyks6040ExfxA5NXPbmkmXJHtQQs9UNg2AKDyyO/XV2BWq0IU3v@@6@@7g7SZVM6uMPEBXAOck@dgkQP8BQzJaLt8VjZVnktMtz@QkChoFFpFKZyttgnylgJBZDLIhn4mHSPFyXw7njDg13MNyh5QAP0RqMlhRKxDl4QAcUbwPFPGjy5kfM08l/4BPlt2C8b7TFEEKdMAUb799fSGT6VXyz@XE93nKwsTCGO5S/5UYtF6cFmgdftzeu9DkpZMDpKOzueFiFnRhdU@9GxqMDuIbGGtpSjo7oKksl5Z4D9/D@@rJ6tjepE1yadHN03RaqQCa5vDLRDf3CW1VlGWPdPgqZF2itC20xCmlOp7Tp2WLOZV82jqrXM7@2Sc0ajYbJutbY7@Kk0Yiip9UyiuqSlX0@YKxkoWAsZGO@wW/EOe/jK3Q1Xdf/ "Perl 5 – Try It Online") Takes its arguments in unary, as two strings of `x` characters whose lengths represent the numbers. The divisor comes first, followed by a `,` delimiter, followed by the dividend. The quotient and remainder are returned in the capture groups `\2` and `\5`, respectively. In contrast to the [ECMAScript regex solution](https://codegolf.stackexchange.com/questions/114003/division-and-remainder/222932#222932), this one doesn't have to do anything anywhere near as fancy or mathematically interesting. Just count the number of times \$divisor\$ fits into \$dividend\$ by splitting the divisor to keep two tandem running totals that are both subtracted from \$dividend\$, one that keeps subtracting \$divisor-1\$, and one that keeps subtracting \$1\$ and adding it to the total quotient. We must do a split like this, because regex refuses to repeat a zero-width group more than once (this, along with the limited space to work in, is exactly what prevents it from being Turning-complete). I never wrote a division algorithm in any regex flavor besides ECMAScript before. So it's interesting to now know how they compare in golfed size. ``` x(x*), # \1 = divisor-1; tail = dividend ( # \2 = what will be the quotient ( x # tail -= 1 (?= ( # \4 = running total (?(4)\4) # recall the previous contents of \4, if any \1 # \4 += divisor-1 ) ) )* # Loop the above as many times as possible (zero or more); if # it loops zero times, \4 will be unset (we'll treat that as 0) ) \4?+ # tail -= \4, or leave tail unchanged if \4 is unset (x*) # \5 = remainder ``` # Java, 41 bytes x(x*),((x(?=(\4\1|(?!\5)\1)()))*)\4?+(x*) [Try it online!](https://tio.run/##fVNNc9owEL3nVyyeYZDAMdAmPdTVME2n7aVJOsmhB@AgsAwytuxKIjiT8NvpCpsEEk81A1rte/u9TvgDP0@i1U5mRa4tJPgOZB50QzjWrK1MG3VaLESJyFm/2@3uSlJ2qU9ISUaMTC4mw2cyak0u6WRICaW0SycXo57j7JDdPyvWs1TOYZ5yY@CaSwVPUOuM5Ravh1xGkCFC7q2WagFcL8x4SsEudb4x8L2ci8LKHC3PAM8PmQqImRKbvUi8YJ5HIkCGR8OrdRwLLaI7wSOhYbannSrJwbJ@xpSGdWTj23CzdP4JMWyGlfPol1QCC2sxtU5TKmNiAvF3zVNDPNcPj9KnU2rlgRDb6OAJPdgXD65F6GGGvFVoeqwzUR1323Bb6ba/ubVCK9CslrDarHABDA33/ZDKQoHdFREb@CC0zrVhgwqLpeLpnpHxUrKPl8hAKXFSxZgvuR5PS2Dg2rJ/OQL0YIg/ZzUN4avW/NEEsUxTUvqdslOHjnMNxHmXGBDkF@b4KPR69BRPHJ7s8QQFxOthulOOkynG7/id8EV3ze18iQPMENBBVr3IficVru43zLOaarDRvCB1gvO8eLyN77haCMwTsNhDIRJX89X7VZ6ngitYofcMy1IROUJlDCRhbAAjJHyG1gqen5G20Pm6IB9okAq1sEuCE5X95Bj7dIK1k@Mi3bl/NFZkQb62QYH7ZmPitSPoA/4x8HyQmO9RHodcVrTJctyOfLScol1Dbn5TUjQEkRrR5O0mV8J7E7uBBufw5@725mdrot6yq8Xr9V612yraaQ@qTT2hvVuEsl6ECmnumhXGtk3tDntQCYe7xYY4Pc94OD/vkKnrZZUltBgM6Nl/Cp2o1xgxx8/NxaiMD/e7GNvt7h8 "Java (JDK) – Try It Online") This is a port of the Perl/PCRE regex to a flavor that has no conditionals. Emulating a conditional costs 5 bytes here. The quotient and remainder are returned in the capture groups `\2` and `\6`, respectively. ``` x(x*), # \1 = divisor-1; tail = dividend ( # \2 = what will be the quotient ( x # tail -= 1 (?= ( # \4 = running total \4 # recall the previous contents of \4 only if it is set \1 # \4 += divisor-1 | (?!\5) # match this alternative only if \4 is unset \1 # \4 = divisor-1 ) () # \5 = set to indicate that \4 is set ) )* # Loop the above as many times as possible (zero or more); if # it loops zero times, \4 will be unset (we'll treat that as 0) ) \4?+ # tail -= \4, or leave tail unchanged if \4 is unset (x*) # \6 = remainder ``` # Regex (.NET), 29 bytes (x+),(?=(\1)*(x*))((?<-2>x)*) [Try it online!](https://tio.run/##bVI9b8IwEN3zK66WK9skodB2gqYgdehGpS4dKAMqB0mUxKmdKpFQfju180FArQfLuvfu3bs757JEpUNMkhNVwVrhAavNbJZhyZfsxCtXeHwR8M@pGPFqJATniyf//rkSI3FiS4@8yDSPEtwRMXdovtUadxDAxKGolFS6ee@lAk6jYDIHGvkJwnTSPF1XwLFD4waNBzRuUTCHWhnOKjaisQAXmMfM3QYi0VJSQ6FqnG6LrxA1p7qNR3ujzZs7Bh@/YWJEaVoDJhrheGPyfFOfpuODkj@5Xj9sxglmhyIEP0PjEW7BZl5xHq84a1MzNCPbJ1Iq0ybcmQxRi969PaWKCvRDqQsgHcP6bY2lhknWlP9TQHhwGT@bExvSt0BWMkNSC/Dh4/1t9XpDzlW7HbhuE@kTBrhdVw87de1c@PQzo1smkemQ9IstUBfWNO8Ctv2pODLNaqt9ZMz4aCHiNL21DizPDn6Qn1@PpPssg/6Q90d/v23@W336BQ "PowerShell – Try It Online") This uses .NET's Balanced Groups feature. It returns the quotient and remainder in `\4` and `\3`, respectively. ``` (x+), # \1 = divisor; assert \1 > 0; tail = dividend (?= (\1)* # push \2 onto the stack for each time \1 fits into dividend (x*) # \3 = remainder ) ((?<-2>x)*) # \4 = quotient: pop all \2 from stack, doing \4 += 1 for each ```