Saturday, September 2, 2017

Re: need help with regex and matchlist()

On 02/09/2017 19:46, Jose Caballero wrote:
> Hi,
>
> As this is my first time I try to write a vimscript function that uses
> regular expressions, I am a little bit of lost. Not sure which
> built-in function is the most proper, and/or how to use it.
>
> Let's say I have this line (very familiar for python developers):
>
> def __init__(self, x, y, z):
>
> I am trying to parse that line and end up with a list of input
> options, like this
>
> ['x', 'y', 'z']
>
> Any tip will be more than appreciated.
> Thanks a lot in advance,

I'd use a combination of matchstr() and split(), e.g.:

let s = 'def __init__(self, x, y, z):'
echo split(matchstr(s, 'def.*(\s*self,\s*\zs.*\ze\s*)'), '\s*,\s*')

The regex uses \zs and \ze to delimit the substring of the match
to return, but you might also use a subgroup enclosed between
\( and \).

Life.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment