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

provide a TIO demonstration of the exception being thrown on the is-prime regex, too
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

This regex takes advantage of the (?<name1-name2>pattern) syntax for golf reasons, using it as an equivalent for (?<-name2>)(?<name1>). It makes sure to only do this when the cursor is at the beginning of the input string, so that only empty values are pushed onto \k<name1>. This is used in two places, and the first instance only works in RegexOptions.Compiled mode; without it, the above-described exception is thrown with inputs \$\ge 21\$is thrown with inputs \$\ge 21\$, probably because some of the 1円 empties it pops were captured from elsewhere than the beginning of the string. A third potential place for this trick would seem to be (?<-2>2円)(?<3>), but if an attempt is made to convert it from (?=.*$((?<-2>2円)(?<3>)|){9}) to (?<=((?<3-2>2円)|){9}^.*), the exception is thrown regardless of mode. Seeing as there's no debugger for .NET regex, it would be tricky to dig deeper into the conditional nature of this error.

This regex takes advantage of the (?<name1-name2>pattern) syntax for golf reasons, using it as an equivalent for (?<-name2>)(?<name1>). It makes sure to only do this when the cursor is at the beginning of the input string, so that only empty values are pushed onto \k<name1>. This is used in two places, and the first instance only works in RegexOptions.Compiled mode; without it, the above-described exception is thrown with inputs \$\ge 21\$, probably because some of the 1円 empties it pops were captured from elsewhere than the beginning of the string. A third potential place for this trick would seem to be (?<-2>2円)(?<3>), but if an attempt is made to convert it from (?=.*$((?<-2>2円)(?<3>)|){9}) to (?<=((?<3-2>2円)|){9}^.*), the exception is thrown regardless of mode. Seeing as there's no debugger for .NET regex, it would be tricky to dig deeper into the conditional nature of this error.

This regex takes advantage of the (?<name1-name2>pattern) syntax for golf reasons, using it as an equivalent for (?<-name2>)(?<name1>). It makes sure to only do this when the cursor is at the beginning of the input string, so that only empty values are pushed onto \k<name1>. This is used in two places, and the first instance only works in RegexOptions.Compiled mode; without it, the above-described exception is thrown with inputs \$\ge 21\$, probably because some of the 1円 empties it pops were captured from elsewhere than the beginning of the string. A third potential place for this trick would seem to be (?<-2>2円)(?<3>), but if an attempt is made to convert it from (?=.*$((?<-2>2円)(?<3>)|){9}) to (?<=((?<3-2>2円)|){9}^.*), the exception is thrown regardless of mode. Seeing as there's no debugger for .NET regex, it would be tricky to dig deeper into the conditional nature of this error.

fix missing close-parenthesis in explanation
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

I had already written a .NET regex to match primes in decimal a couple weeks before, but it must be given a hard-coded maximum bound. It's 192 bytes in length (and isn't shortened by reducing its limit from \999ドル\$ to \255ドル\$, but is shortened to 189 bytes by dropping zero-handling ), and does its calculations in unary, after converting the decimal input to unary. (Its numeric variables are represented by the number of captures on each stack, being agnostic to the contents of the captures.) At the time, I thought it would be impossible to do this without a bound.

I had already written a .NET regex to match primes in decimal a couple weeks before, but it must be given a hard-coded maximum bound. It's 192 bytes in length (and isn't shortened by reducing its limit from \999ドル\$ to \255ドル\$, but is shortened to 189 bytes by dropping zero-handling, and does its calculations in unary, after converting the decimal input to unary. (Its numeric variables are represented by the number of captures on each stack, being agnostic to the contents of the captures.) At the time, I thought it would be impossible to do this without a bound.

I had already written a .NET regex to match primes in decimal a couple weeks before, but it must be given a hard-coded maximum bound. It's 192 bytes in length (and isn't shortened by reducing its limit from \999ドル\$ to \255ドル\$, but is shortened to 189 bytes by dropping zero-handling ), and does its calculations in unary, after converting the decimal input to unary. (Its numeric variables are represented by the number of captures on each stack, being agnostic to the contents of the captures.) At the time, I thought it would be impossible to do this without a bound.

after further research, the conditions for the out-of-bounds error are better understood
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

This regex takes advantage of the (?<name1-name2>pattern) syntax for golf reasons, using it as an equivalent for (?<-name2>)(?<name1>). It makes sure to only do this when the cursor is at the beginning of the input string, so that only empty values are pushed onto \k<name1>. This is used in two places, and I currently do not know why the first instance is workingonly works in RegexOptions.Compiled mode; without it, instead of throwing the above-described exception is thrown with inputs [Edit: regex101 now supports the (?<name1-name2>) syntax, and actually throws the error there – see below]; it seems to be that at least\$\ge 21\$, probably because some of the 1円 empties it pops would have beenwere captured from elsewhere than the beginning of the string. TheA third potential place for this trick would seem to be (?<-2>2円)(?<3>) does in fact throw this exception, but if an attempt is made to convert it from (?=.*$((?<-2>2円)(?<3>)|){9}) to (?<=((?<3-2>2円)|){9}^.*), the exception is thrown regardless of mode. As farSeeing as I know there's no debugger for .NET regex, so it maywould be tricky to get todig deeper into the bottomconditional nature of this error.

*regex101 is actuallydoesn't make it possible to enable the Compiled flag, so it throwing anthrows the error like my reasoning said should happen when usingin versions that use the (?<5-1>1円) shorthand trick. Whether that's due to it running the latest version of .NET, or executing the regex in a stricter manner (perhaps due to reading the values of captures during execution), I have not yet determined. Here is commented view of the section modified to avoid this error (making it 4 bytes longer):

This regex takes advantage of the (?<name1-name2>pattern) syntax for golf reasons, using it as an equivalent for (?<-name2>)(?<name1>). It makes sure to only do this when the cursor is at the beginning of the input string, so that only empty values are pushed onto \k<name1>. This is used in two places, and I currently do not know why the first instance is working, instead of throwing the above-described exception [Edit: regex101 now supports the (?<name1-name2>) syntax, and actually throws the error there – see below]; it seems to be that at least some of the 1円 empties it pops would have been captured from elsewhere than the beginning of the string. The (?<-2>2円)(?<3>) does in fact throw this exception if an attempt is made to convert it from (?=.*$((?<-2>2円)(?<3>)|){9}) to (?<=((?<3-2>2円)|){9}^.*). As far as I know there's no debugger for .NET regex, so it may be tricky to get to the bottom of this.

*regex101 is actually throwing an error like my reasoning said should happen when using the (?<5-1>1円) shorthand trick. Whether that's due to it running the latest version of .NET, or executing the regex in a stricter manner (perhaps due to reading the values of captures during execution), I have not yet determined. Here is commented view of the section modified to avoid this error (making it 4 bytes longer):

This regex takes advantage of the (?<name1-name2>pattern) syntax for golf reasons, using it as an equivalent for (?<-name2>)(?<name1>). It makes sure to only do this when the cursor is at the beginning of the input string, so that only empty values are pushed onto \k<name1>. This is used in two places, and the first instance only works in RegexOptions.Compiled mode; without it, the above-described exception is thrown with inputs \$\ge 21\$, probably because some of the 1円 empties it pops were captured from elsewhere than the beginning of the string. A third potential place for this trick would seem to be (?<-2>2円)(?<3>), but if an attempt is made to convert it from (?=.*$((?<-2>2円)(?<3>)|){9}) to (?<=((?<3-2>2円)|){9}^.*), the exception is thrown regardless of mode. Seeing as there's no debugger for .NET regex, it would be tricky to dig deeper into the conditional nature of this error.

*regex101 doesn't make it possible to enable the Compiled flag, so it throws the error in versions that use the (?<5-1>1円) shorthand. Here is commented view of the section modified to avoid this error (making it 4 bytes longer):

demonstrate regex101 throwing error; fix latent bug in TIO PowerShell script (it worked in TIO but not later versions of PowerShell)
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
show a commented view of the `(?<5-1>1円)`-avoiding patch
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
turns out the `(?<5-1>1円)` really is buggy like I thought it should be, except for some reason it works in some .NET implementations.
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-15 bytes on binary version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-18 bytes on binary version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-38 bytes on binary version, using some of the same tricks already used on the decimal version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-14 bytes, and improve the commenting
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-26 bytes
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-4 bytes
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
adapt the bounded regex to the 1-255 specification of this challenge
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
added 48 characters in body
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Improve and flesh out the framing text, and describe the binary version's internal numeric represenation
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
add note about backtracking
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading

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