vim search wildcard match first occurrence
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.
Related videos on Youtube
mazlix
Updated on May 31, 2022Comments
-
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 matchesrel="blahblah" style="textdecoration:none;"
I have tried
rel=".*\{-\}"
but that gives an errornested \{
-
mazlix almost 13 yearscould you expand on that? I tried
/rel="[^"]
and that just returnedrel="b
-
mazlix almost 13 yearsit says pattern not found and if i escape the ?
:/rel=".*\?"
it saysNested \?
-
Peter Tillemans almost 13 yearsmy apologies, I used the perl syntax first before I realised vi uses another syntax. I learned again something new, thanks
-
bandi almost 13 years@mazlix I meant
/rel="[^"]*"
. You need the asterisk to match everything in the quotes. -
mazlix almost 13 yearsoh.. so what's the
[^"]
do exactly?.. i thought the"
in there was to represent the fact that i'm looking for a closing"
-
bandi almost 13 years@mazlix I added the explanation to the answer.
-
jtony almost 4 yearsThis is super useful when I don't want to match the longest string but the first one. Thansk a bunch!