Monday, March 22, 2010

Re: Using wild characters in replace pattern

MaLpAsO wrote:
> I would like to know how I can use a successful pattern match
> containing wild characters in the replace patters.
> Egg:
> I want to search for patterns like "IN-a3-OUT", "IN-11-OUT" and
> replace it with patterns like "IN-a3-", "IN-11-"
> i.e. I will be searching like s/IN-.*-OUT/
> How can I do this replacement

While I'd start by recommending that you read up on regular
expressions (there are several good books, as well as on-line
tutorials that should take you far), Vim's search uses the power
of these regular expressions.

To clarify your issue, you have a regular expression that finds
the things you want, and you want to strip the "-OUT" off the end
of them? There are several possibilities:

:%s/IN-.*\zs-OUT//g
:%s/\(IN-.*\)-OUT/\1/g

Remember that ".*" is greedy, so if you have a line that looks like

IN-a1-OUT IN-b2-OUT

the ".*" will find "a1-OUT IN-b2", leaving you with "IN-a1-OUT
IN-B2" which may not be what you want. You can tweak that atom to
read either

.\{-} (do it non-greedy)
[^-]* (anything that isn't another "-")

Hope this helps,

-tim


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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

No comments: