Expand an encoded string There's the classic run length encoding and decoding.

input   output
a3b2c5  aaabbccccc


And that's fairly straight forward and done before.

The challenge is to also account for a non-standard behavior when multiple characters precede the run length (a single digit from 0-9). Each character before the run length digit (the last digit before a non-digit or end of the string) has that value applied to it individually and printed out in order.

Some test input and output including some edge cases:

input   output
ab3c5   aaabbbccccc
a0b3    bbb  
13b1    111b
a13b1   aaa111b
a123b1  aaa111222b
aa2a1b1 aaaaab



A character sequence ([a-zA-Z0-9]+) must be followed by its run length length ([0-9])
Only valid input needs to be considered (([a-zA-Z0-9]+[0-9])*)

yes, empty string is valid input.

Input is via standard input, output via standard output


This is code golf, number of bytes determines the winner.
Perl/Bash 54 40+1 = 41 bytes

perl -pe's:(\D*\d*)(\d):"\$1=~s/./\$&amp;x$2/egr":ege'


It's basically a regex within a regex. And a bit of magic.

Explanation

The outer regex /(\D*\d*)(\d)/g extracts each run-length encoded group. We capture the stuff to repeat in $1 and the number of repetitions in $2. Now we substitute each such group with the expansion of that group. For that, we evaluate the code "\$1=~s/./\$&amp;x$2/egr" two times (as by the /ee flag on the outer substitution).

The first evaluation will only interpolate the number of repetitions into the string – the other variables are protected by a backslash. So assuming the input a14, we would now have the code $1=~s/./$&amp;x4/egr, which will be evaluated again.

This will apply the substitution to the contents of $1 (the stuff to repeat a1). The substitution matches each character .. The $&amp; variable holds the whole match, which we repeat x4 times. We do this /globally for each match and /return the substituted string rather than modifying the $1 variable (which is read-only). So the result of the inner substitution is aaaa1111.

The -p flag applies the substitution to each input line and prints out the result.