-4
\$\begingroup\$

Write a regular expression substitution string which can base64 decode a string using only a regular expression engine built into the language. Eg:

# Example in python
import re 

print(re.sub(..., ..., "SGVsbG8gd29ybGQK"))  # "Hello world\n"  
#            ^^^  ^^^
#            These count towards your byte count, the rest doesn't

Rules:

  • No libraries
  • No tools like base64 in bash
  • Must be a regular expression
  • Must decode RFC 4648 based base64
  • Any programming language (eg: Python, JavaScript, PHP, Ruby, Awk, Sed)
  • Length of your find string + Length of your replace string = Byte count
  • Shortest byte count wins
\$\endgroup\$
5
  • 3
    \$\begingroup\$ I'm pretty sure this isn't even possible with a single regular-expression-based replacement. \$\endgroup\$ Commented Dec 11, 2018 at 11:14
  • \$\begingroup\$ @JoKing my bad, didn't even realise, updated it to say RFC 4648 standard \$\endgroup\$ Commented Dec 11, 2018 at 11:42
  • 1
    \$\begingroup\$ Although it's possible to do this using multiple regex replacement. \$\endgroup\$ Commented Dec 11, 2018 at 13:47
  • \$\begingroup\$ Would it be possible to change this to regex golf somehow? \$\endgroup\$ Commented Dec 11, 2018 at 14:12
  • 3
    \$\begingroup\$ Hello, I closed this as unclear because you have defined the exact form of base64 to be used, but you haven't clearly defined what constitutes single regex replacement and what would be illegal (the most logical definition based on your scoring would make this an impossible challenge). \$\endgroup\$ Commented Dec 11, 2018 at 14:27

1 Answer 1

2
\$\begingroup\$

Perl 6, 4 + 87 = 91 bytes

S/(.)*/{:64[first(.ord,:k,flat 65..90,97..122,48..57,43,47)for $0].polymod(256 xx*).chrs.flip}/

Try it online!

Assumes that the input is padded with ending =s and uses + and / as 62 and 63. Really, the first half selects everything while the second half is just a code block to translate base64...

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Although impressive, I'm not sure it's valid according to "using only a regular expression engine": It executes perl code inside the replacement part... That said, I'm pretty sure the task can't be done with "pure" RE. \$\endgroup\$ Commented Dec 11, 2018 at 11:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.