# Regex (Perl / PCRE), <s>36</s> 35 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/jK9RsXf8D "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 Turing-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 ``` # Regex (Java), <s>41</s> 40 bytes x(x*),((x(?=(\4\1|(?!\5)\1)()))*)\4?(x*) [Try it online!](https://tio.run/##fVNNc9owEL3nVyyeYZDAMdAmPdTVME2n7aVJOsmhB@AgsAwytuxKIjiT8NvpCpsEEk81A1rte/u9TvgDP0@i1U5mRa4tJPgOZB50QzjWrK1MG3VaLESJyFm/2@3uSlJ2qU9ISUaMTC4mw2cyak0u6WRICaW0SycXI0fZIbl/VqxnqZzDPOXGwDWXCp6g1hnLLV4PuYwgQ4TcWy3VArhemPGUgl3qfGPgezkXhZU5Wp4Bnh8yFRAzJTZ7kXjBPI9EgAyPhlfrOBZaRHeCR0LDbE87VZKDZf2MKQ3ryMa34Wbp/BNi2AwL59EvqQTW1WJqnaZUxsQE4u@ap4Z4rh0epU@n1MoDIbbRwRN6sC8eXIvQwwx5q9D0WGeiOu624bbSbX9za4VWoFktYbVZ4QIYGu77IZWFArsrIjbwQWida8MGFRZLxdM9I@OlZB8vkYFS4qSKMV9yPZ6WwMC1Zf9yBOjBEH/OahrCV635owlimaak9Dtlpw4d5xqI8y4xIMgvzPFR6PXoKZ44PNnjCQqI18N0pxwnU4zf8Tvhi@6a2/kSB5ghoIOsepH9Sirc3G@YZzXVYKN5QeoE53nxeBvfcbUQmCdgsYdCJG7mq/erPE8FV7BC7xmWpSJyhMoYSMLYAEZI@AytFTw/I22h83VBPtAgFWphlwQnKvvJMfbpBGsnx0W6c/9orMiCfG2DAvfNxsRrR9AH/GPg@SAx36M8DrmsaJPluB35aDlFu4bc/KakaAgiNaLJ202uhPcmdgMNzuHP3e3Nz9ZEvWVXi9frvWq3VbTTHlSbekJ7twhlvQgV0tw1K4xtm9od9qASDneLDXF6nvFwft4hU9fLKktoMRjQs/8UOlGvMWKOn5uLURkf7ncxttvdPw "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 (Python<sup>[`regex`](https://github.com/mrabarnett/mrab-regex)</sup> / Ruby), 43 bytes x(x*),((x(?=((?(5)\5)\1))(?=(\4)))*)\4?(x*) [Try it online!](https://tio.run/##dVFNb8IwDL3zK1ykyQktpRVwAUUcd2PSLjtQDtUIkArSKgla@fWd06B9wawoSvye/V6c5uqOtZ526tzUxoG92sTIg2yXYIQZDoddy9oRTxhr2UowtmJzXtDKOff3YsY5H/FitvKsjvibfDHOt0tQIhs0pbVyBwKygTSmNrY/7msDCpQGU@qDZFkyzbI454sBUHiw@g/0caYevcHUytK8H5lJAFscVRADJuh3uin@VaD2VENbJUQG8mQl6NpRioROUrNzejD1pbGMb/Itj4SaTKoH2LzHnqpvIz4ao7RjKsEJJlWCggxsMLbOsPvWPEZy@gic9@AWg9XeIq5rLanbGN5eX9bPEfJfurdpxgLyr7yv@@MujL9n0b@m1u3qi0s/jHKSeR@BQNrgpHUY3xKRyKHUO0CLfhKInhEg8kEmb/IR/WZQvOuOhQ5PDdQfEiHxUGJfqpOX6D4B "Python 3 – Try It Online") - Python <sub><sup>`import regex`</sup></sub> [Try it online!](https://tio.run/##ZVDRSsMwFH3vV9wWJck6u06nD45QBhPfJgzBByulsmymbGlJOqww/PV6m3SbaGhKzrkn555cvX//ajVwWIqNaKpIiU@Yz55nkRb5agqSx16VGyNWKIk9oXWpjT2uSw0SpII4im5ih4tfGHDtUEmDJhgUEEIwDLo/IsmAf4O2ErmGgvMYEhTfg7@DwwEurqOtUJv6w@dyVFjm7sxcFvZit6p9bUBGdZmZkMAISFgcAUdAd2hLXkl4MuyrQyyeLHvujWB/siiVIAzvX8HL8mnx6JNTs/7tIYex5cTWiHMSN6NzUa083FN7qLRUdS855quFqTGhI30@7pIaG8G2dzzxcDx9Xx@HPgVnFaQqCB3/x8@R//3Wudyin8uTZQ@LeZa1DW0GbEhpQxNOaUJvWYrfmLEOpxPG2IClk6RTte0P "Ruby – Try It Online") - Ruby This is a port of the Perl/PCRE regex to flavors that have no support for nested backreferences. Python's built-in `re` module does not even support forward backreferences, so for Python this requires [`regex`](https://github.com/mrabarnett/mrab-regex). Emulating a nested backreference by copying the group back and forth costs 8 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 (?(5)\5) # recall the previous contents of \4 (as copied into \5) if any \1 # \4 += divisor-1 ) ) (?=(\4)) # \5 = \4, to make up for Python's lack of nested backreferences )* # 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 the lengths of `\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 ``` # Regex `🐘` (.NET), 14 bytes (x+),(\1)*(x*) [Try it online!](https://tio.run/##tVOxboMwFNz5ilfkynYIlLRbokiROnRrpS4dUoYoeQQQYGKDEini21PbkJC0HbrUg2W9O9/de5hK7FGqBPP8ROR8KXGLh2g6LXHPFvTEDh4fs88JH7HDiJ/oYuw@i6JKc9y4fOaQaqUUbmAOoUNQSiGVPcdCAiPpPJwBSf0cYRLao@dxOPZoZtFsQLMOBb2IkWH0QEck4@ABHVO9d4WUW0oaa41C04gMilW9TlAxovhZwarsGmEIRbCVoqnU8jEK1quqbiSqYC2ash6oEosb6lMU5Fhu6@RCsYYZ@LiDEHzdgr3jl6j7gnswkCkaT1Nc6lCJHmScCyH1LOBBU27imbWXaY1@IlQNbs/RMZZGZWwNIvDh4/3t9eXOvbnYT9vzLtUWMFf4Tb//QNc051fyuTudPPxbyFehuf8Tzmlb58rUL7XVPk@1n3t@cTWqmrA0Zn3BBJ/wI1W0NU5HSlsOHeQ6trku07nBQX5221//igf94d4P/Xhlf4T29AU "PowerShell – Try It Online") This uses .NET's Balanced Groups feature. It returns the quotient in the capture count of `\2`, and remainder in the length of `\3`.