Description
In the following examples, we want to enclose each of the last 5 matches
of [0-9]+ with a pair of brackets,
i.e., N=5 for this case.
|
| Raw Input
|
| Desired Output
| number 8888
number 777
6666 last fifth 55555 last fourth 4444
no numbers in this line
Last third 3333 last second 2222
Last number 111
no numbers
|
| number 8888
number 777
number 6666 last fifth [55555] last fourth [4444]
no numbers in this line
Last third [3333] last second [2222]
Last number [111]
no numbers
|
|
Script and Comments
Script1 [ 1] /[0-9]/!b
[ 2] :loop
[ 3] /\n.*([0-9]+([^0-9]+|$)){5}$/!{
[ 4] $b final
[ 5] N
[ 6] b loop
[ 7] }
[ 8] P
[ 9] D
[10] :final
[11] s/([0-9]+([^0-9]+|$)){1,5}$/\n&/
[12] h
[13] s/^[^\n]*\n//
[14] s/[0-9]+/[&]/g
[15] x
[16] s/\n.*$//
[17] G
[18] s/\n//
| |
|