Monday, June 21, 2021

Re: Search for pattern in the first few lines of a buffer

On 2021-06-21, Yegappan Lakshmanan <yegappanl@gmail.com> wrote:
> Hi,
>
> On Sun, Jun 20, 2021 at 3:59 AM Lifepillar <lifepillar@lifepillar.me> wrote:
>>
>> I need to programmatically search for a pattern in the first ten lines
>> of a given buffer, and save the match in a variable when found. Say, the
>> pattern is:
>>
>> % KEY = value
>>
>> I need to extract the value if the above appears in the first lines of
>> the buffer. Of course, the state of the buffer's window, such as the
>> cursor's position, etc., and the content of registers should not be
>> affected. How would you do that?
>>
>
> You can use getbufline() to read the first 10 lines of the buffer
> as a list and then use match() to find the index of the item matching
> a pattern in the list. You can then use substitute() or matchstr()
> to extract the value.

That is a simple strategy!

Thanks,
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/sapng8%24vev%241%40ciao.gmane.io.

No comments:

Post a Comment