580

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?

4
  • I think Paul's answer is good. Just to say that "?" does not mean optional in vim (if this is what you want to achieve using "?")
    – LB40
    Aug 20, 2009 at 12:37
  • 16
    @LB, in many languages, .*? means match any character but be non-greedy. That's what he is trying to achieve. Aug 20, 2009 at 12:49
  • Related: How to make regex matchers non-greedy? on Vim SE. Feb 22, 2020 at 22:21
  • For people not knowing the term ungreedy/non-greedy: it is also called lazy
    – G Eitan
    Dec 29, 2020 at 14:51

8 Answers 8

894

Instead of .* use .\{-}.

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

Also, see :help non-greedy

12
  • 59
    Not very intuitive, is this something that only vim does? Dec 8, 2012 at 5:08
  • 116
    Everything has its own regular expression language... that's one of the biggest issues with regex. Apr 17, 2013 at 22:22
  • 46
    Lots of these tools matured around the same time and independently developed their own dialect of a regular expression language. Many of these tools also were trying to solve different problems so it makes sense that the syntax could be -potentially wildly- different across these implementations. We have to accept that this is just how the real world works even though it sometimes makes our lives harder as developers. Luckily many tools at least provide a Perl-compatible implementation of regex these days. Unfortunately Vim is not one of them. Apr 18, 2013 at 17:16
  • 26
    If anyone like myself defaults their search to \v (very magic flag) you'll want to use .{-}.
    – jgillman
    Mar 12, 2014 at 0:55
  • 68
    @Shurane @Ziggy Mnemonic: controls the number of repetitions like {1,3} does (braces). The minus sign - means: repeat as little as possible (little == minus) ;) Mar 16, 2014 at 21:05
74

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

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

just try:

:help non-greedy
49

What's wrong with

%s/style="[^"]*"//g
1
  • 10
    Although, for my own benefit, I'd still like to better understand the ungreedy thing.
    – Mark Biek
    Aug 20, 2009 at 12:33
20

If you're more comfortable PCRE regex syntax, which

  1. supports the non-greedy operator ?, as you asked in OP; and
  2. doesn't require backwhacking grouping and cardinality operators (an utterly counterintuitive vim syntax requirement since you're not matching literal characters but specifying operators); and
  3. you have [g]vim compiled with perl feature, test using

    :ver and inspect features; if +perl is there you're good to go)

try search/replace using

:perldo s///

Example. Swap src and alt attributes in img tag:

<p class="logo"><a href="/"><img src="/caminoglobal_en/includes/themes/camino/images/header_logo.png" alt=""></a></p>

:perldo s/(src=".*?")\s+(alt=".*?")/$2 $1/

<p class="logo"><a href="/"><img alt="" src="/caminoglobal_en/includes/themes/camino/images/header_logo.png"></a></p>
2
  • 1
    perldo works great, but unfortunately does not highlight the selected test while typing the regex.
    – mljrg
    Feb 8, 2019 at 10:25
  • you can't use perldo for interactive regex find/replace like you can with the native vim substitute s/. Or is it possible? I'd love to be wrong about that.
    – Z4-tier
    Oct 5, 2020 at 23:15
14

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.

2
  • 2
    good point, but being able to do /pattern to check that you're matching the pattern correctly before applying it and using c modifier in your vim regular expression is also nice :) Dec 30, 2010 at 15:00
  • this is correct. all solutions here are not close to non-greedy! if you have to match [0-9]\{7} in a line with lots of text and several occurences of that pattern, no solution here will do. The solutions here only work for simple things (which to be fair, is what was asked). but if you are doing a little more than search till the next quotation, vim won't help.
    – gcb
    Jan 28, 2014 at 17:54
6

With \v (as suggested in several comments)

:%s/\v(style|class)\=".{-}"//g
0
5

Plugin eregex.vim handles Perl-style non-greedy operators *? and +?

2
  • @xsilenT github.com/othree/eregex.vim : "It is recommended to install the script using Vundle or pathogen."
    – eXe
    Nov 9, 2019 at 18:13
  • sorry for that I don't know how to using Vundle or pathogen.
    – xsilen T
    Nov 13, 2019 at 12:11
-3

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,

4
  • 8
    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. Aug 20, 2009 at 17:08
  • 1
    @Brian, cheers. I'll do a help regex and see what I've been missing.
    – Rob Wells
    Aug 20, 2009 at 18:22
  • @RobWells, Sed & Awk, which is indeed a very good book imho, does not explicitly spend any words on greedy/lazy quantifiers. As a proof, there is absolutely no occurrence of the words greed or greedy in the book, and there's only one, but unrelated, occurrence of the word lazy.
    – Enlico
    Apr 18, 2020 at 18:30
  • @EnricoMariaDeAngelis it is but the example does not refer to the term explicitly. It is about how to tailor your regex to use the "not" operator to achieve non greedy matches. The term greedy and lazy arrived with Perl's NFA engine when they introduced operators to specifically modify greedy match behaviour.
    – Rob Wells
    Apr 20, 2020 at 11:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.