Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

add Perl, Java, and mrab-regex
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

Regex (Java / .NET) / Retina, 43 bytes

Try it online! - Java (very slow)
Try it online! - Regex (.NET)
Try it online! - Retina

Regex (Pythonregex / .NET) / Retina , 47 bytes

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

Try it online! - Python import regex
Try it online! - .NET
Try it online! - Retina

This is a port of the 43 byte Java/.NET version, avoiding a nested backreference in order to add Pythonregex support. The way in which this is done (placing the copy inside the lookbehind) removes Java support.

Regex (Perl / PCRE2 v10.35+), 49 bytes

Try it online! - Perl (slow)
Attempt This Online! - PCRE2 v10.40+

(?=
 (
 (2円x+?|^x)
 ((?<= # (?3) calls this
 (?= # Circumvent the constant-width limitation of lookbehinds
 # in PCRE by using a lookahead inside the lookbehind
 ^2円+$ # This is the payload of the emulated variable-length
 # lookbehind, same as the one in the .NET regex
 |
 (?3) # Recursive call - this is the only alternative that can
 # match until we reach the beginning of the string
 )
 . # Go back one character at a time, trying the above
 # lookahead for a match each time
 ))
 )+
)
(2円x+)
4円+$

Regex (Java / Pythonregex / .NET) / Retina , 50 bytes

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

Try it online! - Java (very slow)
Try it online! - Python import regex
Try it online! - .NET

This is a port of the 43 byte Java/.NET version, avoiding a nested backreference in order to add Pythonregex support, while keeping Java support.

Regex (Perl / PCRE), 58 bytes

Try it online! - Perl (slow)
Try it online! - PCRE1
Try it online! - PCRE2 v10.33

Attempt This Online! - PCRE2 v10.40+


Regex (.NET) / Retina, 43 bytes

Try it online! - Regex (.NET)
Try it online! - Retina

Regex (PCRE2 v10.35+), 49 bytes

Attempt This Online! - PCRE2 v10.40+

(?=
 (
 (2円x+?|^x)
 ((?<= # (?3) calls this
 (?= # Circumvent the constant-width limitation of lookbehinds
 # in PCRE by using a lookahead inside the lookbehind
 ^2円+$ # This is the payload of the emulated variable-length
 # lookbehind, same as the one in the .NET regex
 |
 (?3) # Recursive call - this is the only alternative that can
 # match until we reach the beginning of the string
 )
 . # Go back one character at a time, trying the above
 # lookahead for a match each time
 ))
 )+
)
(2円x+)
4円+$

Regex (PCRE), 58 bytes

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

Attempt This Online! - PCRE2 v10.40+

Regex (Java / .NET) / Retina, 43 bytes

Try it online! - Java (very slow)
Try it online! - Regex (.NET)
Try it online! - Retina

Regex (Pythonregex / .NET) / Retina , 47 bytes

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

Try it online! - Python import regex
Try it online! - .NET
Try it online! - Retina

This is a port of the 43 byte Java/.NET version, avoiding a nested backreference in order to add Pythonregex support. The way in which this is done (placing the copy inside the lookbehind) removes Java support.

Regex (Perl / PCRE2 v10.35+), 49 bytes

Try it online! - Perl (slow)
Attempt This Online! - PCRE2 v10.40+

(?=
 (
 (2円x+?|^x)
 ((?<= # (?3) calls this
 (?= # Circumvent the constant-width limitation of lookbehinds
 # in PCRE by using a lookahead inside the lookbehind
 ^2円+$ # This is the payload of the emulated variable-length
 # lookbehind, same as the one in the .NET regex
 |
 (?3) # Recursive call - this is the only alternative that can
 # match until we reach the beginning of the string
 )
 . # Go back one character at a time, trying the above
 # lookahead for a match each time
 ))
 )+
)
(2円x+)
4円+$

Regex (Java / Pythonregex / .NET) / Retina , 50 bytes

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

Try it online! - Java (very slow)
Try it online! - Python import regex
Try it online! - .NET

This is a port of the 43 byte Java/.NET version, avoiding a nested backreference in order to add Pythonregex support, while keeping Java support.

Regex (Perl / PCRE), 58 bytes

Try it online! - Perl (slow)
Try it online! - PCRE1
Try it online! - PCRE2 v10.33

Attempt This Online! - PCRE2 v10.40+


add notes about speed
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

This is not just the shortest, but the fastest under RegexMathEngine, even though the 43 byte .NET regex it's based on is the slowest under .NET (beaten by both 44 byte regexes in speed).

As far as speed goes, the smallest-to-largest version is faster than the largest-to-smallest version (both in .NET and their ports below). Both are faster than the 43 byte version under .NET.

This is a port of the secondlargest-to-smallest 44 byte .NET regex, using lookinto instead of variable-length lookbehind. Although the Perl/PCRE syntax for lookahead conditionals is more verbose, this is compensated for by being able to use a possessive quantifier.

This is a port of the firstsmallest-to-largest 44 byte .NET regex, again using lookinto instead of variable-length lookbehind. This one is less conducive to being ported, and I had to use (?^0=), lookinto accessing the in-progress current match. I've not decided how to finalize the current behavior of this, so it's likely this version will not work with a future version of lookinto.

This is a port of the second 44 byte .NET regex, using lookinto instead of variable-length lookbehind. Although the Perl/PCRE syntax for lookahead conditionals is more verbose, this is compensated for by being able to use a possessive quantifier.

This is a port of the first 44 byte .NET regex, again using lookinto instead of variable-length lookbehind. This one is less conducive to being ported, and I had to use (?^0=), lookinto accessing the in-progress current match. I've not decided how to finalize the current behavior of this, so it's likely this version will not work with a future version of lookinto.

This is not just the shortest, but the fastest under RegexMathEngine, even though the 43 byte .NET regex it's based on is the slowest under .NET (beaten by both 44 byte regexes in speed).

As far as speed goes, the smallest-to-largest version is faster than the largest-to-smallest version (both in .NET and their ports below). Both are faster than the 43 byte version under .NET.

This is a port of the largest-to-smallest 44 byte .NET regex, using lookinto instead of variable-length lookbehind. Although the Perl/PCRE syntax for lookahead conditionals is more verbose, this is compensated for by being able to use a possessive quantifier.

This is a port of the smallest-to-largest 44 byte .NET regex, again using lookinto instead of variable-length lookbehind. This one is less conducive to being ported, and I had to use (?^0=), lookinto accessing the in-progress current match. I've not decided how to finalize the current behavior of this, so it's likely this version will not work with a future version of lookinto.

-1 byte on one of the ports
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

Regex (Perl/PCRE+(?^=)RME ), 4342 bytes

^(?!(x(?=(x*$x*))(?^=(?(?=2円+$)(3円?+2円)))))*$)

Regex (Perl/PCRE+(?^=)RME ), 43 bytes

^(?!(x(?=(x*$)(?^=(?(?=2円+$)(3円?+2円)))))*$)

Regex (Perl/PCRE+(?^=)RME ), 42 bytes

^(?!(x(?=(x*))(?^=(?(?=2円+$)(3円?+2円))))*$)
use a somewhat better looking small-lettering method, keeping the heading itself of the 44 byte .NET regex normal-sized to visually indicate it as a new section
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
it was just a matter of the backtrack limit being too low - it does work in PCRE2 v10.33; and adopt the small-lettering and bold customs I've taken up
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
improve the tone of the beginning; make the display/discussion of version numbers consistent with my more recent posts
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
deleted 1 character in body
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
add lookinto port of the first 44 byte .NET regex
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
add lookinto versions
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
apply the same fix to the TIO PHP link for the 73 byte version (as already applied to the ATO PHP link)
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
fix the PHP PCRE2 v10.40+ test harness on the 73 byte version to no longer crash on numbers >30, and to show error messages
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
add a working PCRE2 v10.40+ test harness for the 73 byte version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
fix a latent bug in the new PCRE1 TIO
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
replace the regex101 test harnesses (especially PCRE1) with ATO ones; replace the C# TIOs with PowerShell ones; fix the consistency of the language specifications
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
add regex101 link to just-added PCRE2 73 byte version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Add PCRE2 port of the .NET regex based on Martin Ender's regex
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Add another .NET version, based on Martin Ender's 45 byte one, but reversed to get 44 bytes
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading

AltStyle によって変換されたページ (->オリジナル) /