Tuesday, January 2, 2024

Re: Function to find all matches in a string?

On Saturday, December 23, 2023 at 2:35:52 PM UTC-8 Tim Chase 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?

Thanks!

-tim

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


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

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/326eaaa2-334b-4fff-999c-86e95a72bff2n%40googlegroups.com.

No comments:

Post a Comment