Monday, November 22, 2010

Re: Regular Expression question

On Mon, 22 Nov 2010, Jeff Lanzarotta wrote:

> Hello,
>
> I have a regular expression question.
>
> I am looping through a file looking for specific lines containing
> specific words.  Here is the basic logic.
>
>
> Step 1. get line.
>
> Step 2. does line contain word1.  Yes, goto step 3, No goto step 1.
> Step 3. does line contain word2.  Yes, goto step 1, No goto step 4.
> Step 4. print line.
>
>
> For example, I have some lines:
>
> 1. word1 sometext
>
> 2. word1 sometext word2 sometext
> 3. word1 sometext
> 4. word1 sometext
> 5. word1 sometext word2 sometext
> 6. word1 sometext word2 sometext
>
> All I want is lines 1, 3, and 4.
>
> word1 sometext
> word1 sometext
> word1 sometext
>
> I know there is a regular expression test to see if the line contains
> word1 but not word2, I just can not figure it out.

You're looking for a "negative lookahead":

:help /\@!

For your case:

/^.*word1\&\(.*word2\)\@!

Lines containing word1 & also not containing word2

:help /\&

If word2 always follows word1 if it's present, you can simplify that to:

/word1\(.*word2\)\@!

The other version handles the case where word2 might appear before
word1, too.

--
Best,
Ben

--
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

No comments: