I want to try a new type of regex golf challenge, which asks you to solve nontrivial computational tasks with nothing but regex substitution. To make this more possible and less of a chore, you will be allowed to apply several substitutions, one after the other.
We'll start simple: given a string containing two positive integers, as decimal numbers separated by a
,, produce a string containing their sum, also as as a decimal number. So, very simply
should turn into
Your answer should work for arbitrary positive integers.
Every answer should be a sequence of substitution steps, each step consisting of a regex and a replacement string. Optionally, for each of those steps in the sequence, you may choose to repeat the substitution until the string stops changing. Here is an example submission (which does not solve the above problem):
Regex Modifiers Replacement Repeat? \b(\d) g |$1 No |\d <none> 1| Yes \D g <empty> No
Given the input
123,456, this submission would process the input as follows: the first substitution is applied once and yields:
Now the second substitution is applied in a loop until the string stops changing:
1|23,|456 11|3,|456 111|,|456 111|,1|56 111|,11|6 111|,111|
And lastly, the third substitution is applied once:
Note that the termination criterion for loops is whether the string changes, not whether the regex found a match. (That is, it might also terminate if you find a match but the replacement is identical to the match.)
Your primary score will be the number of substitution steps in your submission. Every repeated substitution will count for 10 steps. So the above example would score
1 + 10 + 1 = 12.
In the (not too unlikely) case of a tie, the secondary score is the sum of the sizes of all steps. For each step add the regex (without delimiters), the modifiers and the substitution string. For the above example this would be
(6 + 1 + 3) + (3 + 0 + 2) + (2 + 1 + 0) = 18.
You may use any regex flavour (which you should indicate), but all steps must use the same flavour. Furthermore, you must not use any features of the flavour's host language, like replacement callbacks or Perl's
e modifier, which evaluates Perl code. All manipulation must happen exclusively through regex substitution.
Note that it depends on your flavour and modifiers whether each single replacement replaces all occurrences or only a single one. E.g. if you choose the ECMAScript flavour, a single step will by default only replace one occurrence, unless you use the
g modifier. On the other hand, if you're using the .NET flavour, each step will always replace all occurrences.
For languages which have different substitution methods for single and global replacement (e.g. Ruby's
gsub), assume that single replacement is the default and treat global replacement like a
If your chosen flavour is either .NET or ECMAScript, you can use Retina to test your submission (I'm being told, it works on Mono, too). For other flavours, you will probably have to write a small program in the host language which applies the substitutions in order. If you do, please include this test program in your answer.