Thursday 12 September 2019

regex - How can I make my match non greedy in vim?



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




stuff here




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?


Answer



Instead of .* use .\{-}.



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



Also, see :help non-greedy


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...