On Tue 12-Dec-06 2:26pm -0600, Bram Kuijper wrote:

> I am quite new to vim and just started to use regular expressions to
> replace certain amounts of text. For example, in the following piece of
> text I would like to keep only the first column and delete the rest:
>
>   optimal_value_viability       | real    | default 0.0
>   number_generations            | integer | default 0
>   output_per_number_generations | integer | default 0
>   number_trait_loci             | integer | default 0
>   number_pref_loci              | integer | default 0
>   number_viability_loci         | integer | default 0
>   random_seed                   | bigint  | default 0
>
> so I execute the following regexp as a vim command:
>
> :%s/\s+\|.*//
>
> however, to my surprise all text is gone, instead of just anything
> besides the first column.
>
> if I carry out exactly the same regular expression in a Perl script, it
> works: I keep the text in the first column. Same for doing this regexp
> in jEdit. What's different in vim / What am I doing wrong here?

You're very close.  Your '+' needs to be escaped (or it is
treated as the '+' character).  Your '\|' shouldn't be
escaped (or it is treated like a logical 'OR').

    :%s/\s\+|.*//

Alternately, you could make the regex very magic (with '\v')
and it will work, in this case, just like Perl.

    :%s/\v\s+\|.*//

-- 
Best regards,
Bill

Reply via email to