Description
In the following examples, we want to enclose every match of
[0-9]+ with a pair of brackets
except the last 5 ones, 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] h
[ 9] s/\n.*//
[10] s/[0-9]+/[&]/gp
[11] g
[12] D
[13] :final
[14] s/([0-9]+([^0-9]+|$)){1,5}$/\n&/
[15] h
[16] s/\n.*$//
[17] s/[0-9]+/[&]/g
[18] G
[19] s/\n[^\n]*\n//
| |
|