Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #393 - Perl compatible regular expressions

Created: December 30, 2002 8:16 Complexity: intermediate Author: Anonymous Version: 5.7 Karma: 157/59 Imported from: Tip#393

1. Verify in :ver that +perl or +perl/dyn is compiled in.

2. Install Perl if necessary. On Windows, ActivePerl is required.

3. Type :perldo s/searchme/replaceme/g

Comments

Or if you have ruby compiled in (look for +ruby in :ver output):

rubydo sub! /pattern/,'replacement'

ned--AT--bike-nomad.com , December 31, 2002 10:11


� � � What are the advantages compared with this:

1,$s/searchexpression/replaceexpression/gc ?

� � � That now you can have the exact syntax of perl regex�?


detlef--AT--lindenthal.com , January 5, 2003 10:41


The advantage is that when you know Perl regex well its easier to write Perl regex than vi regex. Either is find for simple expressions, but when the expressions get more complex its much easier to work with the syntax you know the best.

myrddin--AT--cwpn.org , January 7, 2003 11:26


one advantage is that perl isn't stuck with / as a separator

s|http://www.example.com/%7Chttp://cheese.com/%7Cg

works and is clearer that a bunch of dogs teeth \/ \/

vimtips--AT--iamafreeman.com , January 11, 2003 15:59


Vim has this feature too. You can have any character as separator.

hari_vim at yahoo dot com , January 11, 2003 20:47


Perl regexes also have a different set of "special characters"

For example, the brackets () are special characters that automatically do grouping and capturing in perl. In a vi regex, they need to be escaped \( \) before they'll turn special.

Again as another poster mentioned, whatever you're most intimate with probably gives you a bit more power.


webmaster--AT--topfx.com , February 21, 2003 18:07


Does anyone know if there's an easier way to do perl based filtering of lines? I do it like this: %!perl -ne "print if (/searchstring/)"

Anonymous , March 21, 2003 9:40


PCRE are much more powerful and faster than POSIX compatible, like used in vi (afaik). Just take a look at perlre [1].

[1] http://perldoc.com/perl5.8.0/pod/perlre.html

t.goedderz--AT--gmx.net , May 17, 2003 9:45


OK, this RULES! Trying to remember the differences between vi REs and perl REs drives me nuts. vi REs are so chunky and inconsistent (like * vs. \+).

Is there any way of turning on perldo by default? I'd like to use perl REs everywhere!

brons_vim--AT--rinspin.com , August 19, 2003 19:35


Skellefte� AIK Sux!

admin--AT--aik4ever.com , January 24, 2004 16:08


Ah, but keep in mind that :perldo is not always enough. Just try to replace something w/ a newline (:perldo s/<text>/\n/<CR>). I get ^--AT-- character instead of a line break. Yeah, i asked for it but not what i wanted.

In this particular case, just use the old "perl pie" ...

:%!perl -pi -e 's/<text>/\n/' 


- Parv

parv , March 6, 2004 21:55


VimTip6

sds , July 6, 2004 4:34


Script to search using PCRE in VIM: http://groups.yahoo.com/group/vim/message/49561

Anonymous , September 18, 2005 22:10


I know this is a small thing, but you might check out "\v", aka very-magic as a way to make the regular expressions less annoying and more perl-like. It would be nice if I could put set very-magic in my vimrc...

Anonymous , September 4, 2006 13:44


Advertisement