vim search wildcard match first occurrence

18,780

Solution 1

You can use:

rel=".\{-}"

\{-} is used for non-greedy match in VIM

Solution 2

Try [^"] instead of .. The latter is "greedy" and will match as many characters as possible.

The [d-r13579] in regexps is used to match "character classes": in this case any small case letter in the range from d to r or an odd digit. If you start the class with a ^ then it negates the meaning.

Thus [^"] means a character except a double quote, and "[^"]*" means two double quotes with any number of arbitrary characters between them, except double quotes.

Solution 3

By default regex matchers are greedy.

  s/rel=".\{-}"/aaaaaaa/

works for me

{-} means short circuit the expression to match the shortest pattern.

Share:
18,780

Related videos on Youtube

mazlix
Author by

mazlix

Updated on May 31, 2022

Comments

  • mazlix
    mazlix almost 2 years

    I have a file that <a href="blah.com" rel="blahblah" style="textdecoration:none;">blah</a>

    I want to match rel="blahblah"

    but when i do \rel=".*" it matches rel="blahblah" style="textdecoration:none;"

    I have tried rel=".*\{-\}" but that gives an error nested \{

  • mazlix
    mazlix almost 13 years
    could you expand on that? I tried /rel="[^"] and that just returned rel="b
  • mazlix
    mazlix almost 13 years
    it says pattern not found and if i escape the ? :/rel=".*\?" it says Nested \?
  • Peter Tillemans
    Peter Tillemans almost 13 years
    my apologies, I used the perl syntax first before I realised vi uses another syntax. I learned again something new, thanks
  • bandi
    bandi almost 13 years
    @mazlix I meant /rel="[^"]*". You need the asterisk to match everything in the quotes.
  • mazlix
    mazlix almost 13 years
    oh.. so what's the [^"] do exactly?.. i thought the " in there was to represent the fact that i'm looking for a closing "
  • bandi
    bandi almost 13 years
    @mazlix I added the explanation to the answer.
  • jtony
    jtony almost 4 years
    This is super useful when I don't want to match the longest string but the first one. Thansk a bunch!