Description
In the following examples, we want to enclose each of the first 5 matches
of [0-9]+ with a pair of brackets,
i.e., N=5 for this case.
|
| Raw Input
|
| Desired Output
| first 1111
no numbers
second 222
third 333 fourth 44444
no numbers
fifth 5555 sixth 666666
number 7777
number 88888
no numbers
|
| first [1111]
no numbers
second [222]
third [333] fourth [44444]
no numbers
fifth [5555] sixth 666666
number 7777
number 88888
no numbers
|
|
Script and Comments
Script1 [ 1] /[0-9]/!b
[ 2] :loop0
[ 3] /([0-9]+([^0-9]+|$)){5}/!{
[ 4] $!N
[ 5] $!b loop0
[ 6] }
[ 7] s/([0-9]+([^0-9]+|$)){1,5}/&\n/
[ 8] h
[ 9] s/\n[^\n]*$//
[10] s/[0-9]+/[&]/g
[11] x
[12] s/^.*\n//
[13] G
[14] s/^([^\n]*)\n(.*)/\2\1/
[15] :loop1
[16] n
[17] b loop1
| |