Finding all words

36 views
Skip to first unread message

Salman Halim

unread,
Oct 3, 2024, 9:51:35 AMOct 3
to Vim Users
Hello,

Is there a way to compose a regular expression in Vim that will find all the words I want, irrespective of the order in which they occur? For example:

Some text...
The quick red fox jumps over the lazy brown dogs.
Some other text...

I want to find 'quick', 'brown', 'lazy' and 'jumps'. I don't know the order in which they occur, so something like 'quick.*brown.*lazy.*jumps' won't work. I'm trying to avoid a complicated/ugly expression that includes all possible orderings like '\%(brown.*lazy\)\|\%(lazy.*brown\)'.

I'm hoping there is a more graceful way to do this already. I'm happy to write a function to convert a sequence of words to an expression, even if it's the 'all combinations' one.

Thank you,

--
 
Salman

I, too, shall something make and glory in the making.

Mikhail Velikikh

unread,
Oct 3, 2024, 10:09:29 AMOct 3
to vim...@googlegroups.com
Hi,

I would use something like this:
\v<(quick|brown|lazy|jumps)>

\v - very magic
< - the beginning of a word
> - the end of a word
(quick|brown|lazy|jumps) - the words to find

Thanks,
Mikhail Velikikh



--
--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CANuxnEcUM43Z%3DOVF0cPXRGq_12NckWMHZBtdH0S_DpKOFJZUOg%40mail.gmail.com.

Tim Chase

unread,
Oct 3, 2024, 10:18:16 AMOct 3
to vim...@googlegroups.com
On 2024-10-03 15:09, Mikhail Velikikh wrote:
> > Is there a way to compose a regular expression in Vim that will find all
> > the words I want, irrespective of the order in which they occur?
>
> I would use something like this:
> \v<(quick|brown|lazy|jumps)>

That finds *any* of the words, not *all* of the words as the OP requested.

While the implementation can often end up slow if you have a large
text or lots of find-them-all terms, you (OP) can use

:help /\&

to join the conditions like

/.\{-}quick\&.\{-}brown\&.\{-}lazy\&.\{-}jumps/

-tim





Salman Halim

unread,
Oct 3, 2024, 11:59:16 AMOct 3
to vim...@googlegroups.com
Thank you; that's exactly what I was looking for. It does slow down, though, as you said it might, once I add more words to it. I might write something that does the combination thing I was suggesting and time the two approaches, but with more than just two words: /quick.*brown\|brown.*quick/ vs. /.\{-}quick\&.\{-}brown}/ 

Best regards,

Salman

Tim Chase

unread,
Oct 3, 2024, 12:09:36 PMOct 3
to vim...@googlegroups.com
On 2024-10-03 11:58, Salman Halim wrote:
> > /.\{-}quick\&.\{-}brown\&.\{-}lazy\&.\{-}jumps/

> something that does the combination thing I was suggesting and time the two
> approaches, but with more than just two words: /quick.*brown\|brown.*quick/
> vs. /.\{-}quick\&.\{-}brown}/

At the cost of some redundancy, you might be able to speed it up a
little bit by requiring that it start with at least one of the words
like

/\%(\<\%(quick\|brown\|lazy\|jumps\)\>\)\@=\%(.\{-}quick\&.\{-}brown\&.\{-}lazy\&.\{-}jumps\)/

so it won't start looking for the other terms until it's found at
least one of them, cutting out a lot of non-starter cases.

-tim





Salman Halim

unread,
Oct 3, 2024, 12:55:42 PMOct 3
to vim...@googlegroups.com

Very good idea. Thanks again.

Salman

--
--
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+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages