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

Hi,

I guess most of us use the forward slash (/) to separate the sections of a match/substring replacement (i.e. s/potato/apple/g). What is the most widely used separator when you have the forward slash in the strings? (i.e s|potato/tomato|apple/orange|g)

I ask out of curiousity as well as if I write a chunk of code, what separator would be easiest for someone else to glance at it without have to change 'modes' (oh, he's using "|" instead of my (insert favorite))

Jason L. Froebe

No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

  • Comment on question regarding separators for regular expressions

Replies are listed 'Best First'.
Re: question regarding separators for regular expressions
by Zaxo (Archbishop) on Aug 02, 2004 at 22:57 UTC

    I like the matched delimeters; parens, brackets, and braces. They minimize or eliminate backwhacking, which eases the "leaning toothpick" effect. You don't even need to escape the delimeters if their use in your regex is balanced.

    After Compline,
    Zaxo

Re: question regarding separators for regular expressions
by BrowserUk (Patriarch) on Aug 02, 2004 at 23:58 UTC

    Usually s[...][...].

    Very occasionally s<...><...>

    For me, always balanced for the reasons Zaxo gave , but also because they highlight in a different colour to the contents of the regex and so stand out.

    I use the same for all other quote-likes also for consistancy.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: question regarding separators for regular expressions
by Aristotle (Chancellor) on Aug 02, 2004 at 22:54 UTC

    I never use a pipe because it's a metacharacter in regular expressions and forces me to parse more carefully as I read the code. Since I want the delimiter to be tall and skinny, if that's possible, the ! bang is my delimiter of choice when the slash won't do. If that still leads to backslashitis, I use pairing delimiters, usually curlies.

    Makeshifts last the longest.

Re: question regarding separators for regular expressions
by ccn (Vicar) on Aug 02, 2004 at 22:43 UTC

    I use curly brackets or commas s{ ... }{ ... }g or s, ... , ... ,g;

Re: question regarding separators for regular expressions
by borisz (Canon) on Aug 02, 2004 at 22:51 UTC
    I like  ':' or '!'.
    s:/::; s!/!!;
    Boris
Re: question regarding separators for regular expressions
by Roy Johnson (Monsignor) on Aug 03, 2004 at 01:01 UTC
    Curlies. Or for the old-fashioned syntax, s#foo#bar#.

    We're not really tightening our belts, it just feels that way because we're getting fatter.

      I used to do that, but I find that the hash marks tend to visually “smother” the text they surround, particularly when it is short, as in your example. Try putting several short substitutions like that in a row:

      s#foo#bar#g, s#bar#baz#g for @line; $line[0] =~ s#baz#quux#g; $line[1] =~ s#qoox#frobble#g; s#firble#flar#g, s#fooz#bish#g for @line;

      Most people's eyes will glaze over. I know mine do.

      Makeshifts last the longest.

Re: question regarding separators for regular expressions
by Fletch (Bishop) on Aug 03, 2004 at 00:17 UTC

    Yet another curly user; except on the command line where I tend use "," since it's handy in that perl, zsh, and sed all can deal with it and it stands out fairly well against paths (e.g. !!:gs,/home/fletch/,/usr/local/,).

Re: question regarding separators for regular expressions
by water (Deacon) on Aug 03, 2004 at 02:22 UTC
    I like s#old#new#;.