Function to find all matches in a string?

119 views
Skip to first unread message

Tim Chase

unread,
Dec 23, 2023, 5:35:52 PM12/23/23
to Vim Users
I was looking for something similar to Python's re.findall()/finditer()
function to answer a question on Reddit[1] but was surprised I
couldn't find anything in

:help function-list

The general intent would be to take an input string and return a
List containing all the sub-strings matching a pattern like

let matches=findall('amwenxipyuqz', '[aeiou]')

would set matches to

['aw', 'en', 'ip', 'uq']

I was able to cobble together a hack-job in this case, but I'd hoped
to be able to do something like

:let a=[] | g/#\w\+/call extend(a, findall(getline('.'), @/))

Does such a function exist and I just missed it?

Thanks!

-tim

[1]
https://www.reddit.com/r/vim/comments/18pcd84/the_vim_way/kenedxq/





aro...@vex.net

unread,
Dec 24, 2023, 8:44:23 AM12/24/23
to vim...@googlegroups.com
> I was looking for something similar to Python's re.findall()/finditer()
> function to answer a question on Reddit[1] but was surprised I
> couldn't find anything in
>

Are you using Linux, MacOS ,or Windows?

Tim Chase

unread,
Dec 24, 2023, 9:45:47 AM12/24/23
to vim...@googlegroups.com
In this case, it was FreeBSD (and I'm uncertain about the author
of the post on Reddit), and there are a bunch of shell-based or
programming-language-based solutions to the particular problem.
My question was more about the (non-)existence of an internal

func(str, regex) -> List of matches

type function.

-tim





Yegappan Lakshmanan

unread,
Dec 25, 2023, 1:12:21 AM12/25/23
to vim...@googlegroups.com, vim_dev
Hi Tim,

On Sat, Dec 23, 2023 at 2:35 PM Tim Chase <v...@tim.thechases.com> wrote:
I was looking for something similar to Python's re.findall()/finditer()
function to answer a question on Reddit[1] but was surprised I
couldn't find anything in

  :help function-list

The general intent would be to take an input string and return a
List containing all the sub-strings matching a pattern like

  let matches=findall('amwenxipyuqz', '[aeiou]')

would set matches to

  ['aw', 'en', 'ip', 'uq']

I was able to cobble together a hack-job in this case, but I'd hoped
to be able to do something like

  :let a=[] | g/#\w\+/call extend(a, findall(getline('.'), @/))

Does such a function exist and I just missed it?


Currently no such function exists.  I have created PR https://github.com/vim/vim/pull/13766
to add support for the matchall() function.  Can you try that out?

Thanks,
Yegappan

Arun

unread,
Jan 2, 2024, 3:36:02 PMJan 2
to vim_use
I think substitute() is the closest, perhaps you can use submatch() for appending to the list. Something like this in mind:
  :let a=[] | g/#\w\+/call substitute(getline('.'), @/, '\=extend(a, [submatch(0)])', 'g')

Regards,
-Arun

Arun

unread,
Jan 3, 2024, 4:31:48 PMJan 3
to vim_use
On Saturday, December 23, 2023 at 2:35:52 PM UTC-8 Tim Chase wrote:
[Retrying.. as my first attempt to post did not seem to go anywhere. Apologies if this is a repeat]

It looks like substitute() is the closest to what you want to achieve. Perhaps you can add submatch() to get the result appended to the list. Something like this in mind:
   :let a=[]|g/#\w\+/call substitute(getline('.'), @/, '\=extend(a, [submatch(0)])', 'g')

Regards,
-Arun 
Reply all
Reply to author
Forward
0 new messages