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

Answers


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')")

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')

Need Your Help

Hibernate many-to-many mapping and cascade=delete

java hibernate orm

I have a mapping (only important parts):

Keeping in sync with database

c# sql-server .net-3.5

The solution we developed uses a database (sqlserver 2005) for persistence purposes, and thus, all updated data is saved to the database, instead of sent to the program.