Manipulation of strings in vim
In my opinion Vimscript has not a lot of features for manipulating strings. I often use matchstr(), substitute() en less often strpart(). Maybe there's more than that.
What is p.e. the best way to remove all text between linenumbers in the next string "a"?
let a = "\%8l............\|\%11l..........\|\%17l.........\|\%20l...." --> etc
I want to keep only the digits and put them in a list. (the text between linenumbers can be diffent)
EDIT: output I want to obtain: ['8', '11', '17', '20'] --> etc
You're looking for split()
echo split(a, '[^0-9]\+')
Given the new constraint: only the numbers from \%d\+l, I'd do:
echo map(split(a, '|'), "matchstr(v:val, '^%\\zs\\d\\+\\zel')")
NB: your vim variable is incorrectly formatted, to use only one backslash, you'd need to write your string with single-quotes. With double-quotes, here you'd need two backslashes. So, with
let b = '\%8l............\|\%11l..........\|\%17l.........\|\%20l....'
echo map(split(b, '\\|'), "matchstr(v:val, '^\\\\%\\zs\\d\\+\\zel')")
One can take advantage of the substitute with an expression feature (see :help sub-replace-\=) to run over all of the target matches, appending them to a list.
:let l= | call substitute(a, '\\%\(\d\+\)l', '\=add(l,submatch(1))[1:0]', 'g')