Monday, November 22, 2010

Re: Regular Expression question

On Nov 22, 2:39 pm, Jeff Lanzarotta <delux256-...@yahoo.com> 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.
>

I think the easiest solution would be use two separate checks. Use
(line=~#'word1' && line!~#'word2') for example.

A better way (depending on what specifically you are trying to do)
could be to use a :v command to automatically loop over every line not
matching word2, and check for word1 before printing:

:v/word2/if getline('.')=~#'word1' | print | endif

You could reverse this if it makes things faster:

:g/word1/if getline('.')!~#'word2' | print | endif

:help :g
:help :v
:help expr-=~#

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