need help with regex and matchlist()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

need help with regex and matchlist()

Jose Caballero
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,
Jose

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: need help with regex and matchlist()

Lifepillar
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: need help with regex and matchlist()

Nikolay Aleksandrovich Pavlov
2017-09-02 23:06 GMT+03:00 Lifepillar <[hidden email]>:

> 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 \).

matchstr() does not ignore &ignorecase, so that needs to be at least

    echo split(matchstr(s, '\Cdef.*(\s*self,\s*\zs.*\ze\s*)'), '\s*,\s*')

(note \C).

But note that regex is not a proper tool for this job: e.g. given
regex is not able to handle annotations, default values or multiline
function signatures. Handling such things is generally not possible
with Vim regexes at all: Vim regexes can’t match balanced parenthesis
and this is required to handle both annotations and default values
properly. Better use Vim with +python[3] or a Python script
communicated with either by older system() or newer job API: Python
both provides `ast` module to parse its code and higher-level things
like `getargspec()` function from `inspect` module.

>
> 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 [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.