On Jun 13, 2010, at 6:59 AM, yosi izaq wrote:
> 
> On Thu, Jun 10, 2010 at 6:06 PM, Yosi Izaq <[email protected]> wrote:
>> Hi,
>> 
>> I'm familiar with the %s/patt//n "trick" to count the # of patterns in
>> the file. I would like to ask how to count the # of paragraphs in a
>> file.
>> My question has 2 flavors:
>> 1. Count free text paragraphs separated by empty lines
>> 2. Count structured paragraphs that have a prefix and suffix
>> delimiters, such as
>> [
>> l1
>> l2
>> l3
>> ]
>> 
>> [
>> l4
>> l5
>> ]
>> 
>> I tried search as follows: /[\_p\{-}\]
>> and it matches well, however the search I tried didn't match: %s/[\_p\
>> {-}\]/n

I tested against this text:

======= text starts here =======
[
l1
l2
l3
]
[
l4
l5
]

l1
l2
l3
l4

l1
l2
l3
l4
l5



====== text ends here ========

The following command finds four matches on the previous text:

:%s/\(\[\_p\{-}]\)\|\(\%^\|^\n\)\@<=\(\p\+\n\)\{-1,}\ze\(^\n\|\%$\)//n

Explanation:

:%s
  /           start pattern
    \(        start group
      \[      literal [
      \_p     any printable + \n
      \{-}    0 or more, as few as posible
      \]      literal ]
    \)        end group
  \|          or
    \(        start group
      \%^     start of file
    \|        or
      ^\n     an empty line
    \)        end group
    \@<=      match starts after previous group
    \(        start group
      \p\+\n  a non-empty line
    \)        end group
    \{-1,}    1 or more, as few as posible
    \ze       match ends before the next group
    \(        start group
      ^\n     empty line
    \|        or
      \%$     end of file
    \)        end group
  //n

It was fun writing that, but I suppose that somebody else can find a shorter 
alternative.

Cheers!
Israel

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

Reply via email to