Skip to main content
update mrab-regex link; use the bee emoji I've established to mean "outputs by number of matches"; establish using elephant emoji for "outputs by capture count"
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52

Regex 🐝 (ECMAScript or better), 12 bytes

Regex 🐘 (.NET), 15 bytes

Regex (Perl / Java / PCRE / Pythonregexregex / Ruby / .NET), 33 29 bytes

Regex (Perl / PCRE / Pythonregexregex), 63 bytes

Regex (Perl / PCRE / Boost / Pythonregexregex), 65 bytes

Regex (ECMAScript or better), 12 bytes

Regex (.NET), 15 bytes

Regex (Perl / Java / PCRE / Pythonregex / Ruby / .NET), 33 29 bytes

Regex (Perl / PCRE / Pythonregex), 63 bytes

Regex (Perl / PCRE / Boost / Pythonregex), 65 bytes

Regex 🐝 (ECMAScript or better), 12 bytes

Regex 🐘 (.NET), 15 bytes

Regex (Perl / Java / PCRE / Pythonregex / Ruby / .NET), 33 29 bytes

Regex (Perl / PCRE / Pythonregex), 63 bytes

Regex (Perl / PCRE / Boost / Pythonregex), 65 bytes

-4 bytes on PCRE/Python/Ruby by using a new approach
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52

Regex (Perl / Java / PCRE2 v10.34 or later / .NET), 29 bytes

((?=((?>\2?)(x*))x(x\3)+$)x)*

Try it online! - Perl
Try it online! - Java
Attempt This Online! - PCRE2 v10.40+
Try it online! - .NET

Regex (Perl / Java / PCRE2 v10.34 or later / .NET), 29 bytes

Uses an atomic group (?>\2?) insteadObsoleted by the 29 byte regex below, which supports a superset of possessive quantifier \2?+ to support 4 different regex engines.

Regex (Perl / Java / PCRE / Pythonregex / Ruby / .NET), 3333 29 bytes

((?=.*(?=(\3?)=\3$|^)(\2(x*)x+)x(x\4\2+$)+$)x)*

Try it online!Try it online! - Perl
Try it online!Try it online! - Java
Try it online!Try it online! - PCRE1
Try it online!Try it online! - PCRE2 v10.33

Attempt This Online!Attempt This Online! - PCRE2 v10.40+
Try it online!Try it online! - Python import regex
Try it online!Try it online! - Ruby

Try it online!Try it online! - .NET

Copies a value back and forth between two groups, to avoid using aFor regex engines that lack nested backreference, to add support for PCRE1 (which would otherwise force the group to be atomic) and Python/Ruby (which don'tbackreferences but support nestedforward-declared backreferences). This automatically adds , we switch to a different approach.NET support too In this version, since the possessive quantifier is no longer needed with an atomiceach iteration, while inside a lookahead being used to copy, stores the new value of tail in the capture group \3 into, so that the next iteration can then recall that value rather directly. Since this directly sets \2\3 instead of building it up incrementally, there's no need to emulate a nested backreference.

                     # tail = N = input number;
                     # no need to anchor, as all inputs return a result
(
    (?=
        (?=.*(\3?)=\3$|^)   # \2tail = a copy of \3, or 0N if \3 is unset
        (\2(x*x+)(\2+$)    # \4\2 = {conjectured largest proper divisor of tail-\2} - 1;
                    #tail; \3 = \2 + \4; tail -= \3; note that the subtraction of 1 from this\2
                    # divisor compensates for the "tail -= 1" on each iteration (below)
        x(x\4)+$    # assert tail - 1 is divisible by \4 + 1
    )
    x               # head +=Increment 1;the tailreturn -=1value
)*                   # Loop the above as many times as possible, minimum zero

Regex (Perl / Java / PCRE2 v10.34 or later / .NET), 29 bytes

((?=((?>\2?)(x*))x(x\3)+$)x)*

Try it online! - Perl
Try it online! - Java
Attempt This Online! - PCRE2 v10.40+
Try it online! - .NET

Uses an atomic group (?>\2?) instead of possessive quantifier \2?+ to support 4 different regex engines.

Regex (Perl / Java / PCRE / Pythonregex / Ruby / .NET), 33 bytes

((?=(?=(\3?))(\2(x*))x(x\4)+$)x)*

Try it online! - Perl
Try it online! - Java
Try it online! - PCRE1
Try it online! - PCRE2 v10.33

Attempt This Online! - PCRE2 v10.40+
Try it online! - Python import regex
Try it online! - Ruby

Try it online! - .NET

Copies a value back and forth between two groups, to avoid using a nested backreference, to add support for PCRE1 (which would otherwise force the group to be atomic) and Python/Ruby (which don't support nested backreferences). This automatically adds .NET support too, since the possessive quantifier is no longer needed with an atomic lookahead being used to copy \3 into \2.

                    # tail = input number; no need to anchor, as all inputs return a result
(
    (?=
        (?=(\3?))   # \2 = a copy of \3, or 0 if \3 is unset
        (\2(x*))    # \4 = {conjectured largest proper divisor of tail-\2} - 1;
                    # \3 = \2 + \4; tail -= \3; note that the subtraction of 1 from this
                    # divisor compensates for the "tail -= 1" on each iteration (below)
        x(x\4)+$    # assert tail - 1 is divisible by \4 + 1
    )
    x               # head += 1; tail -=1
)*                  # Loop the above as many times as possible, minimum zero

Regex (Perl / Java / PCRE2 v10.34 or later / .NET), 29 bytes

Obsoleted by the 29 byte regex below, which supports a superset of regex engines.

Regex (Perl / Java / PCRE / Pythonregex / Ruby / .NET), 33 29 bytes

((?=.*(?=\3$|^)(x+)(\2+$))x)*

Try it online! - Perl
Try it online! - Java
Try it online! - PCRE1
Try it online! - PCRE2 v10.33

Attempt This Online! - PCRE2 v10.40+
Try it online! - Python import regex
Try it online! - Ruby

Try it online! - .NET

For regex engines that lack nested backreferences but support forward-declared backreferences, we switch to a different approach. In this version, each iteration, while inside a lookahead, stores the new value of tail in the capture group \3, so that the next iteration can then recall that value rather directly. Since this directly sets \3 instead of building it up incrementally, there's no need to emulate a nested backreference.

                     # tail = N = input number;
                     # no need to anchor, as all inputs return a result
(
    (?=
        .*(?=\3$|^)  # tail = \3, or N if \3 is unset
        (x+)(\2+$)   # \2 = largest proper divisor of tail; \3 = tail - \2
    )
    x                # Increment the return value
)*                   # Loop the above as many times as possible, minimum zero
add link to Boost bug report
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52

The intent of the 63 byte version was to create a version that works under Boost, but as it turned out Boost has a bug (which I've reported) in which it ignores top-level alternatives in a top-level recursive call (i.e. (?R)), trying only the first top-level alternative. The workaround is to nest the entire regex in a capture group, so that Boost sees the other alternatives. Either (?R) or (?1) would work, but the latter is used for slightly better efficiency.

The intent of the 63 byte version was to create a version that works under Boost, but as it turned out Boost has a bug in which it ignores top-level alternatives in a top-level recursive call (i.e. (?R)), trying only the first top-level alternative. The workaround is to nest the entire regex in a capture group, so that Boost sees the other alternatives. Either (?R) or (?1) would work, but the latter is used for slightly better efficiency.

The intent of the 63 byte version was to create a version that works under Boost, but as it turned out Boost has a bug (which I've reported) in which it ignores top-level alternatives in a top-level recursive call (i.e. (?R)), trying only the first top-level alternative. The workaround is to nest the entire regex in a capture group, so that Boost sees the other alternatives. Either (?R) or (?1) would work, but the latter is used for slightly better efficiency.

add a version that actually works in Boost, and explain why; bold the links where support for another regex engine is first introduced
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
-5 bytes on the recursive Ruby version
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
add Boost to number-of-matches
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
demonstrate the number-of-matches version
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading