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]\+')
EDIT:
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....'
it becomes
echo map(split(b, '\\|'), "matchstr(v:val, '^\\\\%\\zs\\d\\+\\zel')")
>-<
in string "b": \%8l.....\|\%>10l-\%<15l\.....\|\%17l.....
? The result must be `['8','11-14','17'] Thanks in advance : - Reman 2012-04-14 12:53
['8', '11', '12', '13', '14', '17']
Reman 2012-04-14 13:05
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')
substitute()
supports \=
. Thanks - Luc Hermitte 2012-04-06 08:17
>-<
in string "b": \%8l.....\|\%>10l-\%<15l.....\|\%17l...
. ? The result must be ['8', '11-14', '17']
or even better ['8', '11', '12', '13', '14', '17']
Reman 2012-04-15 08:10
\%>10l\%<15l
to match a range of lines (number 9 through 14). For what purpose do you insert a hyphen between those atoms - ib. 2012-04-15 22:26
:call substitute(s:zoek, '\\%[<>]*\(\d\+\)l\.*\\%[<>]*\(\d\+\)l\.*', '\=add(l,(submatch(1)+1)."-".(submatch(2)-1))[1:0]', 'g')
:) --- Why I need the hyphen? I want to keep the ">" and "<" together in only one index - Reman 2012-04-16 10:44