Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Build a replacement ball in regex.

Perl, 114 bytes Takes string and number as space-delimited line on stdin. Prints the regex to stdout. perl -aE'($_,$n)=@F;@s=/./g;$,="|";say grep{$n==y/1//&&s/./$&?".":$s[pos]/eg}map{...

posted 6mo ago by jhnc‭  ·  edited 6mo ago by jhnc‭

Answer
#3: Post edited by user avatar jhnc‭ · 2026-03-18T10:06:24Z (6 months ago)
preprocessing string into an array simplifies access
  • # Perl, 115 bytes
  • Takes string and number as space-delimited line on stdin. Prints the regex to stdout.
  • ```
  • perl -aE'($_,$n)=@F;$x=@s=/./g;$,="|";say grep{$n==y/1//&&s/./$&?".":$s[pos]/eg}map{sprintf"%0*b",$x,$_}1..2**$x-1'
  • ```
  • Comments:
  • ```
  • perl -aE'
  • # 1. load/preprocess string and n
  • ($_,$n) = @F;
  • $x = @s = /./g;
  • # 2. set ouput field separator
  • $, = "|";
  • # 7. print resulting list of strings
  • say
  • grep {
  • # 5. ensure mask has correct number of bits set
  • # else discard
  • $n==y/1// &&
  • # 6. apply mask
  • s/./
  • $& # if mask is nonzero
  • ? "." # then apply
  • : $s[pos] # else use original character
  • # (pos is offset of this match)
  • /eg
  • }
  • # 4. convert to bitmask
  • map { sprintf "%0*b",$x,$_ }
  • # 3. generate all integers from 1 to 2^(length of string)-1
  • 1..2**$x-1
  • '
  • ```
  • Am just learning about powersets so I expect this can be greatly improved.
  • [Try it online!][TIO-mmvulx0e]
  • [TIO-mmvulx0e]: https://tio.run/##RY7BagIxFEX38xUhRG2HzHuOpZuGWFf@hBWJQxRBJ0NeBMXop3eaVNTN5ebdwyGd9fvP/kiWbawJR2/ZiMx5pIrO79qwYXxAP@2A9JRxycRKMsJqCiWiZ/rGzogMG68KMV@Ml/pGKAWi6t8SKdp3PZsrcdIz0gi4VUJqHrlK/mLrbXcRrdZnrBGHQ0qAGH5z4F@CFp2jJdrt9WC6C90/wgfjcs2lOEmxutYAk7IUp6rus41z1TvJalZNGRQmPGqIBoqDc@3jkGo8QA4HOdydnvyPUKyNfz58BANxna7B0ouhEMFCDoIYILUAudkENobsC7RZkCPNDaTWZLAxT@PHHcyyvCTtvxGKX9eFnWupr8z@Dw "Perl 5 – Try It Online"
  • # Perl, 114 bytes
  • Takes string and number as space-delimited line on stdin. Prints the regex to stdout.
  • ```
  • perl -aE'($_,$n)=@F;@s=/./g;$,="|";say grep{$n==y/1//&&s/./$&?".":$s[pos]/eg}map{sprintf"%0*b",@s+0,$_}1..2**@s-1'
  • ```
  • Comments:
  • ```
  • perl -aE'
  • # 1. load/preprocess string and n
  • ($_,$n) = @F;
  • @s = /./g;
  • # 2. set ouput field separator
  • $, = "|";
  • # 7. print resulting list of strings
  • say
  • grep {
  • # 5. ensure mask has correct number of bits set
  • # else discard
  • $n==y/1// &&
  • # 6. apply mask
  • s/./
  • $& # if mask is nonzero
  • ? "." # then apply
  • : $s[pos] # else use original character
  • # (pos is offset of this match)
  • /eg
  • }
  • # 4. convert to bitmask
  • map { sprintf "%0*b",@s+0,$_ }
  • # 3. generate all integers from 1 to 2^(length of string)-1
  • 1..2**@s-1
  • '
  • ```
  • Am just learning about powersets so I expect this can be greatly improved.
  • [Try it online!][TIO-mmvvig5t]
  • [TIO-mmvvig5t]: https://tio.run/##RY7BbsIwEETv/oqVa6BNwy6h6qWRaU78BK2QiQxFgjjymgMi8OlNbRD0MhrPjt9ua/3uvT@whbU14eAtjNgcR6Vo/bYJa5AD/moGrGcgc1DLHJjGM8yIPOgLHImAal8KNV9MvvWFKVdEZf8cm6p50dW8rFgT0qZUuZadLCNcCHY@wBOsnYdgOcCP8Y1lFmLjbXtSjdZHKoiGQ45f1fBTovxQvGgdf5PdnPemPfHtPjmYZCuZV/w6ydXyXCBOs6zicdGnTVKWvcuhgPEMUJhwt6EzKPbONfcg2m6PSRwmcbf29DpEsTL@8fAdGuxWMU2nP2IOHVpMwtgFjC5gcjYWa8P2v2gTIEkc1xhdnYq1eRDfbsUES5OIvRJR/Lo2bF3D/djs/gA "Perl 5 – Try It Online"
#2: Post edited by user avatar jhnc‭ · 2026-03-18T09:43:10Z (6 months ago)
preprocessing string into an array simplifies access
  • # Perl, 123 bytes
  • Takes string and number n on stdin. Prints the regex to stdout.
  • ```
  • perl -aE'($s,$n)=@F;$x=length$s;$,="|";say
  • grep{$n==y/1//&&s/./$&?".":substr$s,pos,1/eg}map{sprintf"%0*b",$x,$_}1..2**$x-1'
  • ```
  • Comments:
  • ```
  • perl -aE'
  • # 1. load string and n
  • ($s,$n) = @F;
  • $x = length $s;
  • # 2. set ouput field separator
  • $, = "|";
  • # 7. print resulting list of strings
  • say
  • grep {
  • # 5. ensure mask has correct number of bits set
  • # else discard
  • $n==y/1// &&
  • # 6. apply mask
  • s/./
  • $& # if mask is nonzero
  • ? "." # then apply
  • : substr $s,pos,1 # else use original character
  • # (pos is offset of this match)
  • /eg
  • }
  • # 4. convert to bitmask
  • map { sprintf "%0*b",$x,$_ }
  • # 3. generate all integers from 1 to 2^(length of string)-1
  • 1..2**$x-1
  • '
  • ```
  • Am just learning about powersets. I expect this can be greatly improved.
  • [Try it online!][TIO-mmv93tw4]
  • [TIO-mmv93tw4]: https://tio.run/##RY5BbsIwEEX3OYVlGSiRM0Oouqll2hWXaCvkRCZFgjjyGAlE4OhNbRCw@fqeeXrjzvrt27Any9bWhL23bELmOFFZ5zdtWDM@ou92RHrBuGRiJRlhsYAc0TN9YUdEhrVXmVh@zX70hVAKRDW8CJKinerPpRIHvbVtE34FKSE177mKB7LG2@4kWq2PWCKOx4SAYvzBOPB32lcUfHJ0jmQ5Rducd6Y70e1PfDTLKy7FQYrVuQSY57k4FOWQvJyrwUlWsmLBIDPhXkNvINs5194HsfY7SOEghbvR8@sSssr4x8P3YKCv4jRYejIUerCQgqAPEFuA1GwEa0P2CdokSBHXNcRWJ7A2D@PrDUyytInaqxGyP9eFjWtpKMz2Hw "Perl 5 – Try It Online"
  • # Perl, 115 bytes
  • Takes string and number as space-delimited line on stdin. Prints the regex to stdout.
  • ```
  • perl -aE'($_,$n)=@F;$x=@s=/./g;$,="|";say grep{$n==y/1//&&s/./$&?".":$s[pos]/eg}map{sprintf"%0*b",$x,$_}1..2**$x-1'
  • ```
  • Comments:
  • ```
  • perl -aE'
  • # 1. load/preprocess string and n
  • ($_,$n) = @F;
  • $x = @s = /./g;
  • # 2. set ouput field separator
  • $, = "|";
  • # 7. print resulting list of strings
  • say
  • grep {
  • # 5. ensure mask has correct number of bits set
  • # else discard
  • $n==y/1// &&
  • # 6. apply mask
  • s/./
  • $& # if mask is nonzero
  • ? "." # then apply
  • : $s[pos] # else use original character
  • # (pos is offset of this match)
  • /eg
  • }
  • # 4. convert to bitmask
  • map { sprintf "%0*b",$x,$_ }
  • # 3. generate all integers from 1 to 2^(length of string)-1
  • 1..2**$x-1
  • '
  • ```
  • Am just learning about powersets so I expect this can be greatly improved.
  • [Try it online!][TIO-mmvulx0e]
  • [TIO-mmvulx0e]: https://tio.run/##RY7BagIxFEX38xUhRG2HzHuOpZuGWFf@hBWJQxRBJ0NeBMXop3eaVNTN5ebdwyGd9fvP/kiWbawJR2/ZiMx5pIrO79qwYXxAP@2A9JRxycRKMsJqCiWiZ/rGzogMG68KMV@Ml/pGKAWi6t8SKdp3PZsrcdIz0gi4VUJqHrlK/mLrbXcRrdZnrBGHQ0qAGH5z4F@CFp2jJdrt9WC6C90/wgfjcs2lOEmxutYAk7IUp6rus41z1TvJalZNGRQmPGqIBoqDc@3jkGo8QA4HOdydnvyPUKyNfz58BANxna7B0ouhEMFCDoIYILUAudkENobsC7RZkCPNDaTWZLAxT@PHHcyyvCTtvxGKX9eFnWupr8z@Dw "Perl 5 – Try It Online"
#1: Initial revision by user avatar jhnc‭ · 2026-03-18T00:15:33Z (6 months ago)
# Perl, 123 bytes

Takes string and number n on stdin. Prints the regex to stdout.

```
perl -aE'($s,$n)=@F;$x=length$s;$,="|";say
grep{$n==y/1//&&s/./$&?".":substr$s,pos,1/eg}map{sprintf"%0*b",$x,$_}1..2**$x-1'
```

Comments:
```
perl -aE'
    # 1. load string and n
    ($s,$n) = @F;

    $x = length $s;

    # 2. set ouput field separator
    $, = "|";

    # 7. print resulting list of strings
    say
        grep {
            # 5. ensure mask has correct number of bits set
            #    else discard
            $n==y/1// &&

            # 6. apply mask
            s/./
                $&                 # if mask is nonzero
                ? "."              # then apply
                : substr $s,pos,1  # else use original character
                                   # (pos is offset of this match)
            /eg
        }

        # 4. convert to bitmask
        map { sprintf "%0*b",$x,$_ }

        # 3. generate all integers from 1 to 2^(length of string)-1
        1..2**$x-1
'
```

Am just learning about powersets. I expect this can be greatly improved.


[Try it online!][TIO-mmv93tw4]

[TIO-mmv93tw4]: https://tio.run/##RY5BbsIwEEX3OYVlGSiRM0Oouqll2hWXaCvkRCZFgjjyGAlE4OhNbRCw@fqeeXrjzvrt27Any9bWhL23bELmOFFZ5zdtWDM@ou92RHrBuGRiJRlhsYAc0TN9YUdEhrVXmVh@zX70hVAKRDW8CJKinerPpRIHvbVtE34FKSE177mKB7LG2@4kWq2PWCKOx4SAYvzBOPB32lcUfHJ0jmQ5Rducd6Y70e1PfDTLKy7FQYrVuQSY57k4FOWQvJyrwUlWsmLBIDPhXkNvINs5194HsfY7SOEghbvR8@sSssr4x8P3YKCv4jRYejIUerCQgqAPEFuA1GwEa0P2CdokSBHXNcRWJ7A2D@PrDUyytInaqxGyP9eFjWtpKMz2Hw "Perl 5 – Try It Online"