Hole type: one-liner
Write a one-liner to read lines of input such as
12:ab:(##:45:X:3).:(31:3):2 and convert all the : to
- but only inside the parentheses. Parentheses will be balanced;
they will not be nested.
e.g.
12:ab:(##:45:X:3).:(31:3):2
->
12:ab:(##-45-X-3).:(31-3):2
Code | Length |
---|---|
perl -pe's/(\(.*?\))/($x=$1)=~y;:;-;;$x/ge' | 43 |
perl -pe'1while s/(\([^)]*):/$1-/' | 34 |
perl -pe's/:(?=[^(]*\))/-/g' | 28 |
The longest solution above uses a translation inside the substitution, while the shortest makes nice use of a positive lookahead assertion. Regular expressions are great for Perl golf!