I'm looking for a regex that will change sth. like this:
print "testcode $testvar \n";
in
printnlog("testcode $testvar \n");
I tried %s/print\s*(.\{-});/printnlog(\1);/g
but gvim says
print\s*(.\{-});
doesn't match.
Where is my fault? Is it ok to use '*' after '\s' because later '{-};' will stop the greed?
Thanks in advance.
This option is useful when you have the '/' character in the search pattern or the replacement string. Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match.
The simplest way to perform a search and replace in Vim editor is using the slash and dot method. We can use the slash to search for a word, and then use the dot to replace it. This will highlight the first occurrence of the word “article”, and we can press the Enter key to jump to it.
Use the slash and dot command to find and replace a single occurrence of a word in Vim.
Using Regex in VimMany of Vim's search features allow you to use regular expressions. Some of them are: The search commands ( / and ? ) The global and substitute command-line (ex) commands ( :g and :s )
In vim you have to prepend (
, )
and |
with backslash, so try
:%s/print\s*\(.\{-}\);/printnlog(\1);/g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With