non-trivial regexp needed, please help

0 views
Skip to first unread message

Peter Princz

unread,
Jan 4, 2010, 9:44:00 AM1/4/10
to vim...@googlegroups.com
Hello VimWorld,
 
first, a Happy New Year to this wonderful list.
 
...but I have a problem, maybe you can help.
 
I am trying to put together a regexp to find pieces of text within one line, but I do not know their order.
 
So far,
/^.*sample1\|sample2.*sample2\|sample1
 
is my best bet but I am still not there.
Also, best if it would be flexible enough to support arbitrary number of samples without much typing.
 
Thank you in advance,
  Peter

Tim Chase

unread,
Jan 4, 2010, 1:12:20 PM1/4/10
to vim...@googlegroups.com
> I am trying to put together a regexp to find pieces of text within one line,
> but I do not know their order.
>
> So far,
> /^.*sample1\|sample2.*sample2\|sample1

Given the lower priority of "\|" in parsing this is

1) any line containing "sample1"
2) or any line containing "sample2" twice
3) or (redundant to #1) any line containing "sample1"

It looks like you want to prioritize your pairings, something like

/^.*\%(\%(sample1.*sample2\)\|\%(sample2.*sample1\)\)

>
> is my best bet but I am still not there.
> Also, best if it would be flexible enough to support arbitrary number of
> samples without much typing.

I don't think there's a natively nice way to do this, but Dr.
Chip has created LogiPat.vim[1] which might simplify some of
these sorts of things. If you have to do this sort of thing
frequently, that would be my first stop.

-tim

[1]
stable at
http://www.vim.org/scripts/script.php?script_id=1290
bleeding edge at
http://mysite.verizon.net/astronaut/vim/ index.html#LOGIPAT

Matt Wozniski

unread,
Jan 4, 2010, 1:49:26 PM1/4/10
to vim...@googlegroups.com
On Mon, Jan 4, 2010 at 1:12 PM, Tim Chase wrote:
>> I am trying to put together a regexp to find pieces of text within one line,
>> but I do not know their order.
...

>> Also, best if it would be flexible enough to support arbitrary number of
>> samples without much typing.
>
> I don't think there's a natively nice way to do this

If I'm understanding the problem correctly, I think you're looking for \&

/.*apple\&.*banana\&.*carrot

See :help /\&

~Matt

Christian Brabandt

unread,
Jan 5, 2010, 9:02:24 AM1/5/10
to vim...@googlegroups.com
Hi Matt!

And if you want to automate this, this could be helpful:
,----
| fu! <SID>SearchMultipleWordsInLine(...)
| if a:0==1
| return a:1
| else
| let a='.*\('.a:1.'\)1'
| let j=a:2
| let b=''
| while j>1
| let b.='\&.*\1'.(a:2-j+2)
| let j-=1
| endw
| return a.b
| endif
| endfu
|
| com! -nargs=+ SearchMulti :let @/=<SID>SearchMultipleWordsInLine(<f-args>)
`----

And then do :SearchMulti sample 4


regards,
Christian
--
Moralisch schw�chelnde Menschen haben in der katholischen Kirche schon
immer ihren Platz gefunden.
-- Harald Schmidt

Peter Princz

unread,
Jan 7, 2010, 3:18:02 AM1/7/10
to vim...@googlegroups.com
Thank you very much for all the responses Tim, Matt and Christian, problem solved!
This below suggested by Matt is good enough and flexible, and yes, it could be automated as per Christian suggested, but I would go for it as it is, it is just a few keystrokes overhead.
Search highlight is a bit weird if turned on, but that is not important at all here, goal is to find a line quickly in a huge file.
 
Once again, thank you very much.
 
Have a nice day,
  Peter

2010/1/4 Matt Wozniski <m...@drexel.edu>

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

Reply all
Reply to author
Forward
0 new messages