Regex only base64 decoder

-3

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

Paradoxis

Posted 2018-12-11T10:09:47.690

Reputation: 95

Question was closed 2018-12-11T14:26:10.530

3I'm pretty sure this isn't even possible with a single regular-expression-based replacement. – Felix Palmen – 2018-12-11T11:14:06.643

@JoKing my bad, didn't even realise, updated it to say RFC 4648 standard – Paradoxis – 2018-12-11T11:42:37.870

1Although it's possible to do this using multiple regex replacement. – user202729 – 2018-12-11T13:47:59.540

Would it be possible to change this to regex golf somehow? – lirtosiast – 2018-12-11T14:12:48.197

3Hello, 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). – HyperNeutrino – 2018-12-11T14:27:15.077

Answers

2

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...

Jo King

Posted 2018-12-11T10:09:47.690

Reputation: 38 234

2Although 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. – Felix Palmen – 2018-12-11T11:41:51.543