39
4
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.
The Challenge
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
47,987
should turn into
1034
Your answer should work for arbitrary positive integers.
The Format
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:
|123,|456
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:
111111
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.)
Scoring
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
.
Miscellaneous Rules
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 sub
vs. gsub
), assume that single replacement is the default and treat global replacement like a g
modifier.
Testing
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.
If anyone has a good idea what to call this type of challenge, leave a comment! :) (Just in case I'm doing more of these in the future.) – Martin Ender – 2015-03-09T10:48:18.210
Those who like this might also enjoy Add without addition and Multiply without numbers
– Toby Speight – 2015-06-05T08:07:23.050Is Retina's regex "flavour" a valid submission? :P (I'm fairly proud of myself for managing to add two numbers at all, let alone golf it.) – totallyhuman – 2017-09-13T15:33:46.130
@icrieverytim Retina's flavour is just the .NET flavour. – Martin Ender – 2017-09-13T15:53:15.873
But Retina has features .NET doesn't have, no? – totallyhuman – 2017-09-13T15:55:09.003
@icrieverytim Not as far as the regex flavour is concerned. Just double-checked what challenge this is, and Retina does have additional substitution features, which would be relevant here. I guess you can use those, but please indicate that those features are newer than the challenge. – Martin Ender – 2017-09-13T15:57:33.060