Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Quite amazing that i get couple of good answers for my previous questions.

I use regex quite often, and used it in various linux tools such as vi, sed, perl, grep... And mostly use in perl. Can somebody tell me how to relate all those ? As because most of the parts of regex are similar in all tools, and some of them are different in different tools. What are all they, and how to relate.

Though it is not very specific to Perl, i believe some Perl expert will have this understanding, so expecting some guidance in that area.

  • Comment on relationship/difference between regex in perl and other unix tools such as grep, sed, vi

Replies are listed 'Best First'.
Re: relationship/difference between regex in perl and other unix tools such as grep, sed, vi
by Anonymous Monk on Aug 05, 2010 at 12:27 UTC
Re: relationship/difference between regex in perl and other unix tools such as grep, sed, vi
by graff (Chancellor) on Aug 06, 2010 at 02:49 UTC
    In general, if I need to use a regex that involves anything like:
    • unicode characters (whether literal or via "\x{HHHH}" notation)
    • unicode character classes (\pL \pM \pP \p{InArabic} etc)
    • procedural manipulation in a regex string replacement (s/.../.../e)
    • "symbolic" classes that I only learned through perl usage (\w, \d, \s)
    • non-capturing groups: (?:...)
    • zero-width assertions, including:
    • look-ahead and look-behind assertions
    • ...
    my inclination is to just use Perl. I wouldn't be surprised if other tools implement some or all of the same things (possibly with the same syntax that Perl uses), but I know Perl does all these things, so it's just less work to use Perl.

    (Also, I know that Perl regexes in 5.10 and later allow a lot of stuff that I haven't even needed to learn yet...)

Re: relationship/difference between regex in perl and other unix tools such as grep, sed, vi
by toolic (Bishop) on Aug 05, 2010 at 15:46 UTC
    This is not the comprehensive answer you are looking for, but, IMO, Perl has better and more exhaustive capabilities than the old unix utilities. On the other hand, grep has the -P switch which allows Perl regex syntax to be used.
Re: relationship/difference between regex in perl and other unix tools such as grep, sed, vi
by repellent (Priest) on Aug 06, 2010 at 04:53 UTC
    Comparison of various regexp syntax: Regexp Syntax Summary

    Learn about Perl regexp: perlrequick, perlretut, perlre

    vim: I began learning about regular expressions from using Vim's regexp, and I can attest to its flexibility + features. When you say vi, you're most likely using vim in vi-compatible mode - so don't limit yourself and do enable Vim's full features (with :set nocp). Vim allows you to visually learn about regexp matching with highlighted search:
    :set hlsearch :set incsearch

    sed: You can replace sed commands with perl. You're better off in the long run learning Perl, IMHO.

    grep: Beyond simple regexp, there's ack - use Perl regular expressions!