Friday, April 2, 2010

Re: Search & Replace

On Fri, 2 Apr 2010, Christian Brabandt wrote:

> Hi Kunal!
>
> On Fr, 02 Apr 2010, Kunal Bajpai wrote:
>
> > To this html code I wanted to select the text between the tags and
> > replace the original text with \n character removed. There is a more
> > complex example that I wanted to ask, here is the problem:
> >
> > Original
> > ______________________________
> > <p>Mike
> > <strong>Gilbert </strong>
> > Senior</p>
> > _____________________________
> >
> > After search & replace:
> > ______________________________
> > <p>Mike <strong>Gilbert</strong> Senior</p>
> > _____________________________
> >
> > I hope you really got what I was asking.
>
> Try this:
> :%s/\(<p>\)\(\w\+\)\_s*\(\w\+\)\_s*\(\w\+\)\(<\/p>\)/\1 \2 <strong>\3<\/strong> \4 \5/
> (one line)
>
> which boils down to:
> :%s/ " substitute on every line
> " anything that matches
> \(<p>\) " the <p> tag and remember it
> \(\w\+\) " a word (which means a string that
> " matches the range 0-9A-Za-z_ but no space
> " and remember it
> \_s* " a space or linebreak
> \(\w\+\) " a word and remember it (see above)
> \_s* " a space or line break
> \(\w\+\) " a word and remember it (see above)
> \(<\/p>\) " the closing tag (and remeber it)
> / " and replace the match part
> \1 " the first remembered string followed by a space
> \2 " the second remembered string followed by a space
> <strong> " the strong tag
> \3 " the third remembered string followed by
> <\/strong> " the closing tag followed by a space followed by
> \4 " the 4th remembered word followed by a space and
> \5 " the 5th remembered word
> /
>

Good explanation, Christian.

Here's one that's more general (not tailored to the specific contents of
that '<p>' tag):

:%s/<\(p\)\_[^>]*>\_.\{-}<\/\1>/\=substitute(submatch(0),"\n"," ","g")/

I put the 'p' in a capture group there so that it could be generalized:

e.g. change it to \w\+ to match all tags. Change it to p|div to match
<p> and <div> tags.

:%s/ " substitute on all lines things matching:
<\(p\) " start tag opener plus tag name
\_[^>]*> " optional things in the start tag, plus start tag closer
" e.g., would match the ' class="big">' of <p class="big">
\_.\{-} " minimal amount of anything-including-newlines until:
<\/\1> " the close tag that matches the open tag name
/\= " replace with the following expression
substitute( " see :help substitute
submatch(0), " the whole match
"\n"," ","g" " with "\n" replaced by " " 'g'lobally
)/

--
Best,
Ben

--
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, reply using "remove me" as the subject.

No comments:

Post a Comment