tags:

views:

1580

answers:

5

I have a big HTML file that has lots of markup that looks like this:

<p class="MsoNormal" style="margin: 0in 0in 0pt;">
  <span style="font-size: small; font-family: Times New Roman;">stuff here</span>
</p>

I'm trying to do a Vim search-and-replace to get rid of all class="" and style="" but I'm having trouble making the match ungreedy.

My first attempt was this

%s/style=".*?"//g

but Vim doesn't seem to like the ?. Unfortunately removing the ? makes the match too greedy.

How can I make my match ungreedy?

+8  A: 

What's wrong with

%s/style="[^"]*"//g
Paul Tomblin
Nothing! Nothing at all wrong! :)
Mark Biek
Although, for my own benefit, I'd still like to better understand the ungreedy thing.
Mark Biek
Rob Wells
+3  A: 

I've found that a good solution to this type of question is:

:%!sed ...

(or perl if you prefer). IOW, rather than learning vim's regex peculiarities, use a tool you already know. Using perl would make the ? modifier work to ungreedy the match.

William Pursell
A: 

G'day,

Vim's regexp processing is not too brilliant. I've found that the regexp syntax for sed is about the right match for vim's capabilities.

I usually set the search highlighting on (:set hlsearch) and then play with the regexp after entering a slash to enter search mode.

Edit: Mark, that trick to minimise greedy matching is also covered in Dale Dougherty's excellent book "Sed & Awk" (sanitised Amazon link).

Chapter Three "Understanding Regular Expression Syntax" is an excellent intro to the more primitive regexp capabilities involved with sed and awk. Only a short read and highly recommended.

HTH

cheers,

Rob Wells
Vim's regex processing is actually quite nice. It can do things that sed can't, like match on line/column numbers or match based on per-language classification of characters as keywords or identifiers or whitespace. It also has zero-width assertions and the ability to put expressions in the right side of a replacement. If you use `\v` it helps clean the syntax up a lot.
Brian Carper
@Brian, cheers. I'll do a help regex and see what I've been missing.
Rob Wells
+14  A: 

Instead of .* use .\{-}.

%s/style=".\{-}"//g

Also, see :help non-greedy

Randy Morris
+6  A: 

Non greedy search in vim is done using {-} operator. Like this:

%s/style=".\{-}"//g

just try:

:help non-greedy
depesz