Function to find all matches in a string?

119 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

Tim Chase

nieprzeczytany,
23 gru 2023, 17:35:5223.12.2023
do 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

nieprzeczytany,
24 gru 2023, 08:44:2324.12.2023
do 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

nieprzeczytany,
24 gru 2023, 09:45:4724.12.2023
do 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

nieprzeczytany,
25 gru 2023, 01:12:2125.12.2023
do 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

nieprzeczytany,
2 sty 2024, 15:36:022 sty
do 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

nieprzeczytany,
3 sty 2024, 16:31:483 sty
do 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 
Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Nowe wiadomości: 0