Re: regexp substitution problem
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
Re: regexp substitution problem
zzapper <[EMAIL PROTECTED]> wrote in news:Xns9897D2B986F7zzappergmailcom@ 80.91.229.5: >:%s/\s\+|.* > > You need to backslash the +, but not the | > > or simpler but possibly morally inferior > :%s/\s*|.* -- zzapper http://successtheory.com/tips/ Vim, Zsh, MySQL Tips
Re: regexp substitution problem
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 ... Whoops! Looks like I removed the first column, but you wanted to keep just the first column. Try :%s/\s*|.*$// which matches any amount of whitespace followed by a bar, followed by anything to end-of-line. Replace with nothing. Regards, Chip Campbell
Re: regexp substitution problem
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. (comments about perl deleted) Let's look at what you've asked vim to do: :% <-- range, all lines s/ <-- substitute \s+ <-- a single whitespace character followed by a + sign \| <-- OR (as in regexp OR regexp ) .* <-- anything // <-- replace with nothing So, your first pattern \s+ , doesn't match any line at all, and hence is wasted effort. Your second pattern, .* , says to greedily match any character, and so matches the entirety of all lines. The "replace with nothing" seems to be understood. OK, presumably this isn't what you had in mind! Vim doesn't accept Perl regexps, it accepts Vim regexps. I suggest reading :help regexp . So, let's construct a pattern that will do what I think you want: :%s/^\s*.\{-}|\s*// which says: any amount of whitespaces, followed by a minimal amount of anything up to a |, followed by any amount of whitespace, replace it with nothing. Since whitespace is clearly matched by ".", in fact :%s/^.\{-}|\s*// will also do the trick. That said; if I were doing this, I'd use visual-blocks: move cursor to upper left hand corner ctrl-v move cursor to bottom right hand corner of region to be removed x Regards, Chip Campbell
Re: regexp substitution problem
Bram Kuijper <[EMAIL PROTECTED]> wrote in news:457F106C.6060207 @rug.nl: > Hi, > > 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+\|.*// > > :%s/\s\+|.* You need to backslash the +, but not the | or simpler :%s/\s\+|.* With your name you should become very good at Vim! -- zzapper http://successtheory.com/tips/ Vim, Zsh, MySQL Tips
regexp substitution problem
Hi, 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? any help is appreciated, Bram