| Raw Input
| ..(.(..(..)..)..)..(..(..)..(..)..)..(..)..
|
|
| Desired Output
| ..\(.(..(..)..)..\)..\(..(..)..(..)..\)..\(..\)..
|
|
Description
To assign a level number to every parenthesis,
we keep a counter whose initial value is zero.
This process scans the line from left to right:
- Whenever a `(' is meet, we increase the counter by one,
and the resulting number is used as its level number.
- Whenever a `)' is meet,
the value of the counter is used as its level number,
then we decrease the counter by one.
For the example above, the level number is shown under every parenthesis:
..(.(..(..)..)..)..(..(..)..(..)..)..(..)..
1 2 3 3 2 1 1 2 2 2 2 1 1 1
|
Script and Comments
Script1 [ 1] s/\(.*$/\n&\n/
[ 2] /\n/!b
[ 3] :loop
[ 4] /\n\(/s/$/#/
[ 5] /\n#$/s/\n/\\\n/
[ 6] /\n\)/s/#$//
[ 7] s/\n(.[^\n()]*)/\1\n/
[ 8] /\n\n/!b loop
[ 9] s/\n//g
| |
|