The purpose of this post is to demonstrate and explain the full list of golfed pure regexes that solve this challenge, and the range of compatibility of each among seven different regex engines, and rank them by length, all together in one post. Others' answers used the best regexes before this post; they are listed below. I ported the **Java** one to three other sets of regex engines, including Python 3's `regex` library. # Regex (Perl / PCRE / Boost / Python<sup>[`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/)</sup>), 11 bytes ``` ^(a(?1)?b)$ ``` [Try it online!](https://tio.run/##TYxBa8MwDIXv/hWiGGJDR5rDLnHTUOh27KnHUWONFAJe2zntYYT0r2eSXNjASM9673vXLsXX@esHdHIwxstniKBLR99mvdsetptJnS7J6L5ZufXG0a7sCP2JLna8pv58g8XHeeEm0BEaGO443Cjuly/VsrLdN5vQ/ruvyLFQg/ZOAeQGYxh@QKlTadvikO5dDVDUxXuIA8nC/mV1dDB5/7bfeT8fTTBtZVu0ep4DqhCQB01ZvLMQ9ZRZo1JB8UUhPbEYV4G@3ILi0BY7o1LNKY4EGQxkQhBmGOIISo305KLchL8 "Perl 5 – Try It Online") - Perl [Try it online!](https://tio.run/##fVJRT9swEH7Przi6MWwWULtKe2ha0IZAe2AwVUzaBCxy0ksaLU0sx1lHUf/6ujs7ZYWHRZF9vvu@785fkmp9lKfp5lVRpWU7Qxjr1ODx/CRI58pArG/vYQLT3sfl8mcybL8/PHy7mb9fzsXmh1DidCBPE/l6I1@WeyEc6kms3w6i4J90Y2dFzdq7KVNU@fNcUVMW1eIkKCoLmgA2RmNqI9K6aiy4yQ4X2DQqR/lIqqNRSgAYj6HLcujyWM3KCAza1lQwiNZOcqGKSkh4DIAefUvdSqyElkeD@0k/gpYww3exhZpPTMgpcGCn6Wemgm5tBOwXHBqMYHc6Gkdb49kGm7a0PuZ7ZFmDNoS6tdQ2t3NfqX9hamtzO7z3rUh14sTjtF7ookShQ@iH8MZL@2Cr1ZeeVGQg9gzK7Y0z514maG5Ch9D7cjY9d0MYUNTT0WF/NoL95q6ir7aj6ft0whkRBI@5Yk@cDTnasqhQeP@LKvSOyIgwg62726FWEqjG3oiDu@qgU/X3ZHe2d8XfmAqDIVx9vbzsFI/TmCwXcntsihXyqe/ezrcQhjui3LITHpM5O8O8qO5NgC2Jz6fT62l8df35w83ZJwmdbb1zdmoEPboUlg0@5S8UnVz@SXj9HHFjWgI8Q2RLU1gU/73TIGRza7bR8dbefYNIE8unP7n7pYL1RiWBUgkvtLqNdx@4qAt9nASBCjgTJPS6EtMDRUdWSVyFdlf2VCfNKIYotzDBMxyFOUxiSOJknI4X8krJnzQrVd5sjkr@zn8B "C++ (gcc) – Try It Online") - PCRE1 [Try it online!](https://tio.run/##XZDNboMwEITvPIVrWYpdkR@ucRGXkipSlVQ0t9JaNjIByRBkyKnqs9Nd01Ml7B3vznwYhmaYn7KhGQjzJCV0S8mGDN5elbeD05Xlq@3mMVOq0W5S1a0bWmd9yUshy2I7rmKyglVDU10tGvrJ9tPIlTocX3OlREwSAUgAy6i@ec7adCeZS2uwj/z98nw8CSm@SVtz5j7GyTvbgxLr5DNNadlTAebxbmAC7XgXrxMhg7sVtmpuaJEEqImMCGFdGi7f6alqOPMxkCRB3/JRTo@Tst7DRcRD@lbkL@p0VnlRnIuM5tjfE7rnrMvoxd/tnsCJHrQbQVKBbwgsyhyVP/9@Chdy/uKaZ4nIjGDzrE2ktcEN9lCwLiKoP7loE0U6wk5k4AkjjEcajkgxYQI1jJdoQKMLLTpsGFgSIYIZDKHFBEzgLKCFZH4B "PHP – Try It Online") - PCRE2 [Try it online!](https://tio.run/##XVDbbpwwEH33V4xopNgtuy2N1AfoZqU@9AOqPLRKUmQ7BqywxjJGNFvl10s8Q65FaDzjM@fM8WjvN63WyzvrdD/dGPiqhmGMH4NpzZ9t5/05050MUPvLa9jBj@zbPN@qs@nX3d3Pi@7L3PHlN5d8X4i9EieL@B/Ocnjvd7X/UFTsZYZNI4KRh3NmXYSDtI4L@Msgff4yQb1x3ItNcb37VMGUes4@1xEGrJDQpoSax3hTlqnfujYBfooV6MGNEegRZUmvgGC4z99claXVcjSierodDzLqDuZOxlW5GQJwnHXEoTSnNbG3znAqtHX5OjKJHHfFk338bAP8KCBhuDp@euVORfWM@uQ2Nvy1nXo0MuiOk15OLvLkWsAesoswmRIggxKy77IfU5G9UmvmYKNZmVtdp11w8WhsO9qjwarI8QEDWiXePcVg4hQcpE3eL1IxKRWGFOnAc00oe0zXXDEmGd4wlX6CkM5kKlFFEZJOglcqSWMXtkgKSFgZREEOkrBFkQzprEKrkvqnm16247LpaX01be8B "C++ (gcc) – Try It Online") - Boost [Try it online!](https://tio.run/##bY5BagMxDEX3PoUwhdiQDhm6czHZ9QTdTVuQG6cxuJ5B49Dm9FNLzrJgpC/p/4eXW73M5WlL38tMFdbbuqf4FX8VgQfSWm8fBs1xtMdgH7Y2T6N7HN@fIfmDOs8EGVLh2LDWUypOAaQzJAcLpVKN5dmPfZtjMdkClhPkqUG8372VnYPs83RgarPdSfO1Dj@UajT6la7RAWgmyNeGNSJ9XgztodFiXiPoF2zNgbb/IbLdMCjEwKVVady7EHWXXQelUPFGhfbkxHGFbWRKkEvrcu5RQbOLLSiFAz0hEc5wiC1BMMLpoE4Kfw "Python 3 – Try It Online") - Python <sub><sup>`import regex`</sup></sub> Included for completeness. Already used in: * [Martin Ender's Mathematica answer](https://codegolf.stackexchange.com/a/86104/17216) * [Titus's PHP answer](https://codegolf.stackexchange.com/a/86365/17216) * [Paul Picard's Perl answer](https://codegolf.stackexchange.com/a/86006/17216) ``` ^ # Assert we're at the beginning of the string. ( # Define subroutine (?1): a # Match one 'a'. (?1)? # Optionally do a recursive subroutine call to (?1). b # Match one 'b'. ) $ # Assert we're at the end of the string. ``` When the regex engine hits the `(?1)?`, it has the choice of whether to do the call or not. Due to `?` being greedy by default, it first tries doing the call; if it then hits a `b` character instead of an `a`, it will backtrack, avoid doing the subroutine call, and then reach the part of the regex that matches a `b` character. From that point forward, it will keep popping out of the recursive call stack, matching another `b` each time. This way, the number of times that it attempts to match `b` equals the number of `a` it matched at the beginning. If it finds an `a` when attempting to match `b`, this will trigger a non-match. If it finishes popping and still finds more `b` characters, this will trigger a non-match at the `$` part of the regex. If it reaches the end of the string prematurely during this stage, it will pop out from that call and attempt to match another `b`, thus triggering a non-match. The only backtracking left to be tried at the popping-out stage, in the case of any of those non-matches, is to stop calling `(?1)` at an earlier point, but this will also fail to match, as it will then be trying to match `a` where the string has a `b`; this will result in a top-level non-match. # Regex (Ruby), 12 bytes ``` ^(a\g<1>?b)$ ``` [Try it online!](https://tio.run/##RYzBasMwEETv@oqpyKE9xMQ9RlVDIO0xh5JbkhotUV2BMEa2SXPJr7vaVaEgdmc18yZNdJsTLD5863/6qvNX7LaHbZW8uxgEuzK4fofoEW3rx0EB4Qvhgf/7aRwMfCe52rARj8v6bK0@ddpkIh5XVbV8Pksqo30K3YgIe0fCBvqQJr8GNNbQ7y4O@dD/sUI1zdt@1zTz56M7tS/164aeFvPsSDlHPPKUxbsIUX@yaFLKKf5RlJ9YjCuXT24hcfIWu6BSzSmOOBkMFEIQZhjiCEmN9JSi0kS/ "Ruby – Try It Online") Included for completeness. Already used in [G B's Ruby answer](https://codegolf.stackexchange.com/a/182021/17216). This is the same as the 11 byte version, but using Ruby's subroutine call syntax. # Regex (<sub><sup>Perl / PCRE / </sup></sub>Java), 21 bytes ``` ^(a(?=a*(\2?+b)))+\2$ ``` [Try it online!](https://tio.run/##TYxBi8IwEIXv@RWDBJqsLrXCXhprEVyPnjyWDZOlQiGrbqoHKfWvd2eShV0IMy/z3veubfBv09cDZDAw@MsnepC5oW@13m2P280oTpegZFctzXpjaBd6gO5EFz1cQ3e@waw5z8wI0kMF/d31N4rbxWuxKHT7zSbU/@5LcjSUIK0RAKlBKYafkMuQ6zo7hntbAmRltkffk8z0X1Z6A6O174edtdOHQlVX@KKaVT13Wut5s5LThE4gOh404@KdRFS/MmknBAq@CEcvWowLpC@3uOjQjnZCYzWnOIJxMJCIiDDDEEdcrIk9qSg1uR8 "Perl 5 – Try It Online") - Perl [Try it online!](https://tio.run/##fVJRT9swEH7Przi6MewSUEulPTQtaEOgPTCYqk7aRFnkuE4aLU0sx1kHqH993Z2ddoWHRZF9vvu@785fIrU@yaTcvMlLWTRzBSMtjTpdnAdyIQzE@v4BxjDpfFytfiaD5vvj47fp4v1qwTY/mGAXY9Fls7OL44Rzfjw7e7vhr4GdELp6HOvjfhT8a1LbeV5Rl/2UycvsZS6vMKvE8jzISwsaATZWxlSGyaqsLbgZu0tV1yJT/BlVh0OJABiNoM1S6PKqnBcRGGUbU0I/WjvJpchLxuE5AHz0PXYrVMk0P@k/jHsRNIgZnMUWKjoRIcPAgZ2mnxkLurERkHPQNSqC/elwHG2NZxtVN4X1Md0jTWtlQ6gai20zu/CV6peStjL3gwffClXHTjyW1VLnhWI6hF4I77y0D7ZaPe5JeQrswCi@vXHq3EsZzo3oEDpfLidXbggDAns6OhzOh3BYz0r8anuavk8rnCKB0ZhP5ImzIVO2yEvFvP95GXpHeISY/tbd7VBPHLBG3rCjWXnUqvp7kjvbu6rfSjKjQrj9enPTKp7KGC1nfHus8ydFp557W99CGOyJUstWeITm7A3zqnowBrIkvppM7ibx7d3nD9PLTxxa2zpX5NQQOngpVdRql78WeHL5nfD6JWJqGgS8QKQrk1vF/nunfkjmVmSj4629@0YpnJjv/uT2lwrWG5EEQiS04Oo22n3gojb0cRIEIqBMkODrSkQPBB5JJXEV3F3ZU500oQgi3EIEz3AU4hCJIImTcTpeyCslf2RaiKzenBT0nf8C "C++ (gcc) – Try It Online") - PCRE1 [Try it online!](https://tio.run/##XZBRb4MgFIXf/RWMkBRa284@lhFfZpcmS7u4vs2NgMFqgtagfVr2290F97REuId7z/lE@7qfntK@7hFxSCC8xWiDemeu0pneqtLQxXazTKWslR1leWv7xhpX0ILxIt8OixgtYFXQlFfjDd1ounGgUh6Or5mULEYJAySAeVTdHCWNeOTEigrsA32/PB9PjLNv1FSU2I9hdNZ0oNg6@RQCFx1mYB7uGibQjh/jdcJ4cDfMlPXNWzgCasIjhEgrwuVbNZY1JS4GEkfeN3@UVcMojXNwEfYg3vLsRZ7OMsvzc57izPf3CO8paVN8cXezR3DCB2UHkJj5NwQWJhbzn38/hTI@fVFFU6GWtNilK80YWxU7Mk1KR0ppv8Eeiq@zCOpPzlpHkYp8J9LwhJGPRwqOnqLDBGoYz9GA9i5vUWHzgTkRIj7jQ96iAyZwZtBM0r8 "PHP – Try It Online") - PCRE2 [Try it online!](https://tio.run/##bZHfb9MwEMff81fcIqTZLWRjj4usSkjsCRCivFEm2YnTukucYDtrUZW/vdzZAW2oUmTfr@/nfJe9fJbv9vXT2XRD7wLs0S9MXyxKeBkZg2kvxpze6iNlhlG1poKqld7DZ2ksnOaQDzLg9dybGjpMsHVwxm5Buq3/8ZND2Ln@4OHjsdJDMD0KM4AH02pohNWHaLK8qPpaF5jPeflhbBrtdP1Ny1o7ULHsdZD9Vc5uw3k59/VvQ3nYEZ8xLxSOIOtPxmrG@ZWwY9ty0zBf6F@jbD3LbxaLRc756XVpIjAWLgJOSAj/CAi4QYLCuqfSL8X1xl7THcopxaavMgTtLDgxWzhtN1ADz0vcxrqS1uKkxg5jAAE03Bxj698@6K4wluPvsQGMuC1hni/WFzvpv@hjwOfBCfBl5krc8lnWY37AtYTWMgKI99QPO6ZdtdgsQSwS5tHx94Zqh8/pMOuKLnmsxcz/1IZ1RWNszTisIP/uRn0PkMM95A@4GnTyCyIiTdOU0ebPj0yylZALtrlbLRXnfLm5e3OmlZ6lyqRUdOAZL7qTEa3ZTLbKMplRJFP4xRTJM4kuUVTM4B3TSRrRVEUlMh4kSIooIQ2JqERFTOQkUCKpPw "Java (JDK) – Try It Online") - Java Turned out to be used in [Kevin Cruijssen's Java answer](https://codegolf.stackexchange.com/a/86061/17216), but gave it a -6 byte golf. This regex is the next-best thing to recursion, in engines that lack it. It works using group-building: ``` ^ # Assert we're at the beginning of the string. ( a # Match one more 'a' per each iteration of this loop. # Lookahead assertion - when this finishes, the regex engine's # "cursor" will jump back to the same position as it was in when # it entered the lookahead here. (?= a* # Skip over as many 'a' as necessary to get to the first 'b'. # Capture \2, embedding within it the previous contents of itself. # If this is the first iteration, \2 will be unset, so capture a # single 'b' in \2. Otherwise, append one more 'b' to the previous # contents of \2. ( \2?+ # Optionally match \2, but if it does match, lock that # match in (don't backtrack to here and try not making the # match, in the case that a non-match occurs after this # point). This is guaranteed to fail to match if and only if # we're on the first iteration, due to \2 being unset. On # subsequent iterations, since \2 will have been captured # starting at the first non-'a' character, and always starts # its attempted match there, it will be guaranteed to match. b # Match one 'b'. ) ) )+ \2 # Match however many 'b' we captured in \2. $ # Assert we're at the end of the string. ``` # Regex (.NET), 22 bytes ``` ^(a)+(?<-1>b)+$(?(1)^) ``` [Try it online!](https://tio.run/##RY3BasMwEETv@gphBJJIXOqr3TSBQG@99VYaWKXrWqDKRnZwifG3u1qpUDA7s955o6GfMYwdOreJcHgP@IU/H3XtcVYnuV0U6J06PpXVs9E7oY6q0he9ydO@OPffg3X4WehG3A@PTdsHhGunhOPWc2H9cJv0Ylsl7nqZg52w7PpxWmO4av53Xvo@PuasR14IZVuuRHh4hena4RjLtOaLfAs3rLlcOboR4/4CUWu5ai5csW5gGIChEWcS0myS@7PZG8aA0R9m4pdOhDOIK7WYdImazhlN1ZSiCKRBQCYSQgxBFDGpJvXkotxkfgE "PowerShell – Try It Online") Included for completeness. Already used in: * [Leaky Nun's Retina answer](https://codegolf.stackexchange.com/a/86002/17216) * [AdmBorkBork's PowerShell answer](https://codegolf.stackexchange.com/a/86011/17216) * [Dion Williams's C# answer](https://codegolf.stackexchange.com/a/86300/17216) (with regex 7 bytes longer than it needs to be) This regex uses the Balancing Groups feature of the .NET regex engine: ``` ^ # Assert we're at the beginning of the string. (a)+ # Match as many 'a' characters as we can, minimum of one, pushing # each single-character capture onto the Group 1 capture stack. (?<-1>b)+ # Pop the Group 1 capture stack as many times as we can, matching # a single 'b' each time. This will stop looping when it hits a # character other than 'b', or empties the Group 1 capture stack. $ # Assert we're at the end of the string. If this fails to match # (meaning there are more 'b' characters than 'a' characters), the # regex engine will backtrack, but none of the choices it has will # be able lead to a match, so there will be a top-level non-match. (?(1)^) # If there are any remaining captures on the Group 1 stack (which # means there were fewer 'b' characters than 'a' chracters), assert # something which can never be true - that we're at the beginning # of the string. Since the first thing the regex did was capture at # least one 'a', the fact that we've reached this point means that # was already done, so it's impossible for us to be at the beginning # of the string. Thus this causes a non-match in the case that there # are fewer 'b' than 'a'. ``` Some people use this regex in the form of `^(a)+(?<-1>b)+(?(1)^)$`, i.e. with the `$` at the end rather than the beginning, but I prefer `^(a)+(?<-1>b)+$(?(1)^)`, since it tries the slightly-less-complicated operation of `$` first, so in the case of a non-match, it will be ever so slightly more efficient. In theory. The `^` in the `(?(1)^)` can be replaced with various other things that can't match, such as `(?(1).)` or `(?(1)c)`. # Regex (<sub><sup>Perl / PCRE / Java / .NET</sup></sub>), 24 bytes ``` ^(a(?=a*((?>\2?)b)))+\2$ ``` [Try it online!](https://tio.run/##TYzNasMwEITveoolCKxtExwHcrFim0DaY085mgqpOGBQ8yMnh2LcV3d3pUILYne0M99cu@C38@cXyKBh9JcP60Hmmr7V7rA/7utJnC5Byb5a612taRc4Qn@iC47X0J/vsGjPCz2B9FDB8HDDneJmuSqWBXY3NqH5d1@Tg1CCNFoApAalGP6GXIYcm@wYHl0JkJXZq/UDyQz/stJrmIx5eTsYM78rq5rKPinV1O2mQYeIz@1GzrN1wlrHg2ZcvJOI6lcm7YSwgi/C0YsW48LSl1tcdGhHO6GxmlMcsXEwkIiIMMMQR1ysiT2pKDW5Hw "Perl 5 – Try It Online") - Perl [Try it online!](https://tio.run/##fVJRT9swEH7Przi6MewSUEulPTQtaEOgPTCYqk7aRFnkuE4aLU0sx1kHqH993Z2ddoWHRZF9vvu@785fIrU@yaTcvMlLWTRzBSMtjTpdnAdyIQzE@v4BxjDpfFytfiaD5vvj47fp4v1qwTY/mGAXY9Fl7OJ8dnbBE8758ezs7Ya/xnZC6OpxrI/7UfCvT23neUWN9lMmL7OXubzCrBLL8yAvLWgE2FgZUxkmq7K24MbsLlVdi0zxZ1QdDiUCYDSCNkuhy6tyXkRglG1MCf1o7SSXIi8Zh@cA8NH32K1QJdP8pP8w7kXQIGZwFluo6ESEDAMHdpp@ZizoxkZA5kHXqAj2p8NxtDWebVTdFNbHdI80rZUNoWosts3swleqX0raytwPHnwrVB078VhWS50XiukQeiG889I@2Gr1uCflKbADo/j2xqlzL2U4N6JD6Hy5nFy5IQwI7OnocDgfwmE9K/Gr7Wn6Pq1wigRGYz6RJ86GTNkiLxXz/udl6B3hEWL6W3e3Qz1xwBp5w45m5VGr6u9J7mzvqn4ryYwK4fbrzU2reCpjtJzx7bHOnxSdeu5tfQthsCdKLVvhEZqzN8yr6sEYyJL4ajK5m8S3d58/TC8/cWht61yRU0Po4KVUUatd/lrgyeV3wuuXiKlpEPACka5MbhX77536IZlbkY2Ot/buG6VwYr77k9tfKlhvRBIIkdCCq9to94GL2tDHSRCIgDJBgq8rET0QeCSVxFVwd2VPddKEIohwCxE8w1GIQySCJE7G6Xghr5T8kWkhsnpzUtB3/gs "C++ (gcc) – Try It Online") - PCRE1 [Try it online!](https://tio.run/##XZBfb4MgFMXf@RSMkBQ6@8c@ljlfZpcmS7e4vs2NgMFqgtagfVr22d0F97REuId7z/mJ9nU/PaR93WPqcILJhuA17p25SGd6q0rDFpv1MpWyVnaU5bXtG2tcwQouinwzLCK8gFVBU16MN3Sj6caBSXk4vmRS8gjHHJAAFqi6OkabZCuoTSqwD@z9/HQ8ccG/cVMxaj@G0VnTgeKr@DNJSNERDubhpmEC7WgbrWIugrvhpqyv3iIwUGOBMKZtEi7fqrGsGXURkAT2vvmjrBpGaZyDi/C75C3PnuXpVWZ5/pqnJPP9PSZ7RtuUnN3N7DGcyEHZASTh/g2BRagl4uffT2FcTF9MsTRRS8bSx2KXcs05vy92dJqURkppv8Eeiq@zCOpPzlojpJDvIA1PGPk4UnD0FB0mUMN4jga0d3mLCpsPzIkQ8Rkf8hYdMIEzg2aS/gU "PHP – Try It Online") - PCRE2 [Try it online!](https://tio.run/##bZHfb9MwEMff81fcIqTZHWRjj4tMJST2BAhR3uiQ7MRp3SVOsJ21qMrf3t3ZYWKoUmTfr@/nfJedfJLvdvXjyXRD7wLs0C9MXyxK@DcyBtOejTm90QfKDKNqTQVVK72HL9JYOM4hH2TA66k3NXSYYKvgjN2AdBv/84FD2Lp@7@HTodJDMD0KM4B702pohNX7aLK8qPpaF5jPeflxbBrtdP1dy1o7ULHsdZD9Vc5uw3k59/VvQ7nfEp8xLxSOIOvPxmrG@YWwY9ty0zBf6N@jbD3LrxeLRc758XVpIjAWzgKOSAgvBARcI0Fh3WPpr8Tl2l7SHcopxaZvMgTtLDgxWzhtN1ADz0vcxqqS1uKkxg5jAAE03Bxjqz8@6K4wluPvsQGMuClhni/WF1vpv@pDwOfBEfBl5kLc8FnWY37AtYTWMgKI99QPO6ZdtdgsQSwS5tHx94Zqi8/pMOuKLnmsxcz/1IZ1RWNszTgsIf/hRn0HkMMd5Pe4GnTyMyIiTdOU0eZPv5hkSyEXjC0/rG@XXHHOr9a3b0601ZNUmZSKDjzjRXcyojWbyVZZJjOKZAq/mCJ5JtEliooZvGM6SSOaqqhExoMESRElpCERlaiIiZwESiT1DA "Java (JDK) – Try It Online") - Java [Try it online!](https://tio.run/##RY1BS8QwEIXv@RWhBJqoK7rHltoFwZs3b67CZHdqAzEpaZfKlv72mkkEocx703nfy@BnDGOP1m4iNO8Bv/Dno6oczvJQbp8SZNvAjZTt03HfKq2Uuj3uxVYe7opn/z0Yi@dC1eLaPNSdDwinXgrLjePCuOEyqcV0UlzVMgcz4a7347TG8GP9v/Od8/E5axzyQkjTcSnC/StMpx7HWKYUX8q3cMGKlytHO2LcXyBqVa6KC1usG2gGoGnEmYQ0m@T@bPaaMWD0h@n4pRPhDOJKLTpdoqZzRlM1pSgCaRCQiYQQQxBFdKpJPbkoN@lf "PowerShell – Try It Online") - .NET This is a port of the **21 byte** Perl/PCRE/**Java** regex, with `\3?+` changed to `(?>\3?)` to allow the regex to also work on .NET (which lacks possessive quantifiers). # Regex (<sub><sup>Perl / PCRE / Java / Ruby / Python<sup>[`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/)</sup></sup></sub>), 29 bytes ``` ^(a(?=a*(?=(\3?+b))(\2)))+\2$ ``` [Try it online!](https://tio.run/##TUzLasMwELzrK5YgsLZJcJzQixXHBNIee8rRVEjFAYOah5wcinF/3d2VCi2ImdHO49oG/zx9foEMGgZ/@bAeZK7pW20P@@N@N4rTJSjZVSu93WniAgfoTnTB4Rq68x1mzXmmR5AeKugfrr9T3CyWxaLA9sYm1P/uK3IQSpBGC4C0oBSXvyGXIcc6O4ZHWwJkZfZqfU8yw7@s9BpGY17eDsZM78qqurJPBKrZ1HOHqJo1Is6btZwm64S1joEwEnMSUf3KpJ0QVvBFOHrR4rqw9OUVFx3iaKdqnOYUR2wELqRGrHCHSxxxcSbupKG05H4A "Perl 5 – Try It Online") - Perl [Try it online!](https://tio.run/##fVJRT9swEH7Przi6MewSUEulPTQtaEOgPTCYqk7aRFnkuE4aLU0sx1kHqH993Z2ddoWHRZF9vvu@785fIrU@yaTcvMlLWTRzBSMtjTpdnAdyIQzE@v4BxjDpfFytfiaD5vvj47fp4v1qwTY/mGAXY9HFhc0GF8cJ52x2xjk/np293fDXhE4IXT2O9XE/Cv41q@08r6jbfsrkZfYyl1eYVWJ5HuSlBY0AGytjKsNkVdYW3KzdpaprkSn@jKrDoUQAjEbQZil0eVXOiwiMso0poR@tneRS5CXj8BwAPvoeuxWqZJqf9B/GvQgaxAzOYgsVnYiQYeDATtPPjAXd2AjIQegaFcH@dDiOtsazjaqbwvqY7pGmtbIhVI3Ftpld@Er1S0lbmfvBg2@FqmMnHstqqfNCMR1CL4R3XtoHW60e96Q8BXZgFN/eOHXupQznRnQInS@Xkys3hAGBPR0dDudDOKxnJX61PU3fpxVOkcBozCfyxNmQKVvkpWLe/7wMvSM8Qkx/6@52qCcOWCNv2NGsPGpV/T3Jne1d1W8lmVEh3H69uWkVT2WMljO@Pdb5k6JTz72tbyEM9kSpZSs8QnP2hnlVPRgDWRJfTSZ3k/j27vOH6eUnDq1tnStyaggdvJQqarXLXws8ufxOeP0SMTUNAl4g0pXJrWL/vVM/JHMrstHx1t59oxROzHd/cvtLBeuNSAIhElpwdRvtPnBRG/o4CQIRUCZI8HUlogcCj6SSuAruruypTppQBBFuIYJnOApxiESQxMk4HS/klZI/Mi1EVm9OCvrOfwE "C++ (gcc) – Try It Online") - PCRE1 [Try it online!](https://tio.run/##XZDNbsIwEITveQrXsoQXwk/oDdfKpVAhVVCl3JrWsiNDkByInHCq@ux07fRUKV6Pd2e@OGnr9v6Ut3VLmCeS0DklM9J6e1Letk5Xlo/ms3GuVK1dr6pr056d9SUvQZTFvBulZITriE11ssFw6e2l77hSm@3rWilISQaIRLBIjlfP2VkuBHPyiPaOvx@etzsQ8E3OR87cR9d7Zy@oYJp9SknLCwU0dzeDE2yni3SagYjuM9iqvgaLIEjNREIIa2S8fKP7qubMp0gSJPiGj3K665X1Hi8CD/KtWL@o3V6ti2Jf5HQd@itCV5w1OT34m10RPNGNdh1KCuENkUWZo@Ln30/hIO5fXPNc6jEWXj7mEwPAyyUATMolu9@1SbQ2oWCNW9gHEdWfHLRJEp2ETmLwiaMQTzQeA8XECe5xPEQjOriCRccSAkMiRkImhILFREzkDKCBZH4B "PHP – Try It Online") - PCRE2 [Try it online!](https://tio.run/##bVHfb9MwEH7PX3GLkGankI3xtsiqhMSeACHKGwXJTpzWXeIE21mLqv7t3Z0dEEOVovv13fed77KTT/LNrnk8m34cXIAd5qUZyqKCfytTMN3FmtMbfSBknFRnaqg76T18ksbCcS75IAO6p8E00CPAVsEZuwHpNv77Dw5h64a9hw@HWo/BDEjMAB5Mp6EVVu9jyPKyHhpdIp7z6v3Uttrp5quWjXagYtvLIvvDnNOW82qe61@Har8lfca8ULiCbD4aqxnnV8JOXcdNy3ypf02y8yy/KYoi5/z4sjUpMBYuChxRIfxVQIEbVFDY91j5hbhe22vyoTql2umLDEE7C07MEW7bjzTA8wqvsaqltbipseMUQAAtN9fY6rcPui@N5fh7bAAjbiuY94v95Vb6z/oQ8HlwBHyZuRK3fKYNiI94ltBZRgLiLc3DielWHQ5LIhYV5tXx94Z6i8/pEXVlnzLWIfK/asv6sjW2YRyWkH9zk74HyOEe8gc8DSb5BRIpnU6njC5//skkWwpZoGHrd8uF4pyt7zjni/XdqzOd9ixVJqUigzY68imI0RymWGWZzKiSKfwiRPRMYkoqKiLoI5yoUZq6qEVGQ4TEiBTiEIlaVJSJOkkoKaln "Java (JDK) – Try It Online") - Java [Try it online!](https://tio.run/##RYzBasMwEETv@oqtyMFqiEncW4QwgSTHHkpucWpWVE0FwhjZJumlv@5oV4GCmJ3Vzps42d85goEPd3X3vuzcDfa7066MDr80eLPWcPvxwUEwVzcOAsB/g3@h/34aBw2u49xG0yGcV5uLMbLppE5EOK/LclVdOJXQPvpuhADmDyLUIE9xclsACVuQRwxDWuR/LFNte3jft@38WWBRG3xNUjRv9dIqVTSVUmrZVIt5RisQLUlSHjSzYfe02VshUNCPsOnxiXCBaaUWy5c0@ZxRrqYURZCFgEwwQgxBFLFcwz25KDfZBw "Ruby – Try It Online") - Ruby [Try it online!](https://tio.run/##bY5NagMxDIX3PoUwhdhNOuRn52Ky6wm6m2lBbpzE4HoGj0Ob008tOcuCkZ6k9z483ct1TIclfE9jLjDf5032F/8rMljIUsrlU6E6WnyuRQ2H49pprYa91no97J@W6uh35mX38QrBbsV5zBAhJAJ1czmFZARAOEMwMOWQitI0213bRp9U1IDpBLGvEGtXQ1oZiDb2W6JW24M03kr3k0PxSr7nmzcAkgj82W72mL@uKm@g0nycPcg3rM2A1P8hol7QCURHpVZu1Jtg9ZBNOyFQ0Ea4@vhEcYF1JIrjS@18blFGk4ssyIUCLcERylCILI4xzGmgRnJ/ "Python 3 – Try It Online") - Python <sub><sup>`import regex`</sup></sub> This is a port of the **21 byte** Perl/PCRE/**Java** regex (see below) adding compatibility with engines that lack nested backreferences, but still have forward-declared backreferences and possessive quantifiers. This allows it to work in Python when using the `regex` library instead of `re`. ``` ^ # Assert we're at the beginning of the string. ( a # Match one 'a' per iteration of this loop. (?= a* # Skip over as many 'a' as necessary to get to the first 'b'. (?= # Capture \2. If this is the first iteration, \3 will be unset, so # capture a single 'b' in \2. Otherwise, \2 = \3 concatenated with # one more 'b'. ( \3?+ b ) ) # \3 = make a copy of \2, which was captured above ( \2 ) ) )+ \2 # Match however many 'b' we captured in \2. $ # Assert we're at the end of the string. ``` # Regex (<sub><sup>Perl / PCRE / Java / Ruby / Python<sup>[`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/)</sup> / .NET</sup></sub>), 32 bytes ``` ^(a(?=a*(?=((?>\3?)b))(\2)))+\2$ ``` [Try it online!](https://tio.run/##TUzLasMwELzrK5YgsLZJcZzSixXbBNIee8rRVEjFAYPyqJwcinF/3d2VCi2ImdHO49oF/zyfvkAGDaO/fFgPMtf0rbb73WFXT@J4CUr21Vpva01c4Aj9kS44XkN/vsGiPS/0BNJDBcPdDTeKm9VjsSqw@2QTmn/3NTkIJUijBUBaUIrL35DLkGOTHcK9KwGyMnu1fiCZ4V9Weg2TMS9ve2Pmd2VVU9kHAqWaun1q0CGqdoOIy3Yj59k6Ya1jIIzEnERUvzJpJ4QVfBGOXrS4Lix9ecVFhzjaqRqnOcURG4ELqREr3OESR1yciTtpKC25Hw "Perl 5 – Try It Online") - Perl [Try it online!](https://tio.run/##fVJRT9swEH7Przi6MewSUEulPTQtaEOgPTCYqk7aRFnkuE4aLU0sx1kHqH993Z2ddoWHRZF9vvu@785fIrU@yaTcvMlLWTRzBSMtjTpdnAdyIQzE@v4BxjDpfFytfiaD5vvj47fp4v1qwTY/mGAXY9HFhbGL89nggiecs9kZ5/x4dvZ2w19zOiF09TjWx/0o@NevtvO8oob7KZOX2ctcXmFWieV5kJcWNAJsrIypDJNVWVtw43aXqq5Fpvgzqg6HEgEwGkGbpdDlVTkvIjDKNqaEfrR2kkuRl4zDcwD46HvsVqiSaX7Sfxj3ImgQMziLLVR0IkKGgQM7TT8zFnRjIyAToWtUBPvT4TjaGs82qm4K62O6R5rWyoZQNRbbZnbhK9UvJW1l7gcPvhWqjp14LKulzgvFdAi9EN55aR9stXrck/IU2IFRfHvj1LmXMpwb0SF0vlxOrtwQBgT2dHQ4nA/hsJ6V@NX2NH2fVjhFAqMxn8gTZ0OmbJGXinn/8zL0jvAIMf2tu9uhnjhgjbxhR7PyqFX19yR3tndVv5VkRoVw@/XmplU8lTFazvj2WOdPik4997a@hTDYE6WWrfAIzdkb5lX1YAxkSXw1mdxN4tu7zx@ml584tLZ1rsipIXTwUqqo1S5/LfDk8jvh9UvE1DQIeIFIVya3iv33Tv2QzK3IRsdbe/eNUjgx3/3J7S8VrDciCYRIaMHVbbT7wEVt6OMkCERAmSDB15WIHgg8kkriKri7sqc6aUIRRLiFCJ7hKMQhEkESJ@N0vJBXSv7ItBBZvTkp6Dv/BQ "C++ (gcc) – Try It Online") - PCRE1 [Try it online!](https://tio.run/##XZDNbsMgEITvPAVFSGFT57e3UNeXOlWkKqnc3OoWgUXiSDixsHOq@uzpgnuqZJZhd@Yzdlu3t8esrVvKPU0pmzE6pa23R@Vt63RlxWg2HWdK1dr1qro07clZX4oSZFnMulFCR7gO2FRHGwzn3p77Tii13rzmSkFCF4BIBEtyuHjBT@lccpce0N6J9/3zZgsSvunpILj76Hrv7BkVTBafacrKMwM0d1eDE2wn82SyABndJ7BVfQkWSZG6kIRS3qTx8o3uq1pwnyBJ0uAbPsrprlfWe7wI3KVvRf6itjuVF8WuyFge@ivKVoI3Gdv7q11RPLG1dh1KBuENkcW4Y/Ln308RIG9fQoss1WMsQmRP5UMGBkCUSwC4L5f8dtOGaG1CwRq3sA8iqj85aEOIJqFDDD5xFOJE4zFQTJzgHsdDNKKDK1h0LCEwJGIkZEIoWEzERM4AGkjmFw "PHP – Try It Online") - PCRE2 [Try it online!](https://tio.run/##bVHfb9MwEH7PX3GLkGZ3kI3tbVGohMSeACHKG2WSnTitO8cJtrMWVfnby50dEEOVovv13fed77ITz@LNrnk66W7oXYAd5oXui0UJ/1bGoM3ZmlMbdSBkGKXRNdRGeA@fhLZwnEs@iIDuudcNdAiwVXDabkC4jf/@g0PYun7v4cOhVkPQPRIzgAdtFLSVVfsYsryo@0YViOe8fD@2rXKq@apEoxzI2PayyP4w57TlvJzn@teh3G9JnzFfSVxBNB@1VYzzi8qOxnDdMl@on6MwnuXXi8Ui5/z4sjUpMBbOChxRIfxVQIFrVJDY91T6q@pybS/Jh3JKtemLCEE5C66aI9y2G2iA5yVeY1ULa3FTbYcxQAW03Fxjq18@qK7QluPvsQF0dVPCvF/sL7bCf1aHgM@DI@DL9EV1w2daj/iAZwnGMhKo3tI8nJhuZXBYErGoMK@OvzfUW3xOh6grupQxg8j/qi3rilbbhnFYQv7NjeoeIId7yB/wNJjkZ0ikNE1TRpc/PTLBlpVYoGFs@W59t@SSc7a@5ZxfrW9fnei6JyEzISQZtNGRT0GM5jDFMstERpVM4hchomcCU1KREUEf4USN0tRFLSIaIiRGpBCHSNQio0zUSUJJSf4G "Java (JDK) – Try It Online") - Java [Try it online!](https://tio.run/##RYzBasMwEETv@oqt6EFqiEncW4RiAmmPPZTc4tSsqJoKhDGyTdpLf93VrgoFMTurnTdpdt9LAguv/uq/hqr3NzgeTocqeXw3EOzGwO0zRA/RXv00CoDwAeGO/od5Gg34nnNbQ4d4Xm8v1sq2lyYT8bypqnV94VRGhxT6CSLYH0jQgDyl2e8AJOxAPmMc8yL/Y4XquqeXY9ctbwpVY/Ehi1LNvn1stNNatbXWetXW98uCTiA6kqw8aBbD7s8W74RAQT/C5ccnwgXmlVocX/Lkc0G5mlIUQRYCCsEIMQRRxHEN95Si0uR@AQ "Ruby – Try It Online") - Ruby [Try it online!](https://tio.run/##bY7BbsMgDIbvPIWFJhW2LmraGxPLbU@wW7JJsNIWiZEIqLY@fYZNj5OQ/dv@/08st3KZ42H138ucCuRb3iZ3dr8sgYbEOV8/hRGDNo@1CDG8TodBWinFtJdSPk37h7Waxl499x8v4PWOneYEAXxEVpfL0UfFAPwJvIIl@ViExFn3bRtcFEGCiUcIY4VovZniRkHQYdwhtdrupPlaup/kixP8PV2dAuBIoP922Zn0dRFpC5XmQnbA30xtCrj8DxHkaiwzxmKplRr2JkjdZdOWMcNww2x9dMI4M3VEiqVL7XRuUUKjCy2GCgZagiKYwRBaLGGI00CNZP8A "Python 3 – Try It Online") - Python <sub><sup>`import regex`</sup></sub> [Try it online!](https://tio.run/##RU3RSsQwEHzPV4QSaFY90fOtpfZA8M033zyFzbm1gdiWtEflSr@9ZhNBCDOz2ZnZoZ/Jjy05tylfvXn6op/3ouho1od8@9Co6wqvAmhdPx4fajAA@rgHgOvjXm354SZ76r8H6@gzg1Jdqruy6T3hqdXKSdtJZbvhPMFiG60usMzeTrRr@3Fag/m@/J/lruvDWWc7kpnStpFa@dsXnE4tjaEMQC75qz9TIfNVkhspzM8YuMhXkMpl64ZGIBqGgJGYk4jqTyZthEDBP8KEF1ccFxhGbjFxEziuUzRWs4stGIEDKREjnOEQW0ysiT2pKDWZXw "PowerShell – Try It Online") - .NET This is a port of the above, with `\3?+` changed to `(?>\3?)` to allow the regex to work on .NET (which lacks possessive quantifiers), so it can support 6 different regex engines at once. It is not supported by Boost, which lacks both forward-declared and nested backreferences. It might not be possible for a single regex to work on all 7 engines.