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

Hi Monks,

Why does this:
  perl -e '$a="<abc|def>";$a =~ s/<(.+?)(\|.+?)>/-$1-/;print $a'
give this output:
  -abc-
which looks right to me, but this:
  perl -e '$a="<abc|def>";$a =~ s|<(.+?)(\|.+?)>|-$1-|;print $a'
(which is the same thing with '|' regex pattern delimiters), give this output:
  -a-

I was wanting to use the regex substitution pattern delimiter '|' instead of '/' because my read data had '/' in the text, so '|' meant I didn't have to escape the '/'s.

Perl version: 5.10.1.

Thanks.
tel2

Replies are listed 'Best First'.
Re: Regex delimiters: '/' vs '|' (escaped vs escaped)
by tye (Sage) on Oct 03, 2014 at 03:11 UTC
    I was wanting to use the regex substitution pattern delimiter '|' instead of '/' because my read data had '/' in the text, so '|' meant I didn't have to escape the '/'s.

    You don't have to escape delimiters in the read data. You only have to escape delimiters in the regex (or replacement). And the regex you show contains no '/' characters but it does contain a '|' character, so '|' is a strange delimiter choice.

    $ perl -MO=Deparse -e 'm/\|/' /\|/; $ perl -MO=Deparse -e 'm|\||' /|/;

    The above shows how you have changed the meaning of your regex by changing the delimiter character. To include a '|' in a regex delimited by '|'s, you have to type '\|'. The '\' escapes the delimiter and leaves an unescaped '|' for the regex. Worse, you can't get the original regex easily:

    $ perl -MO=Deparse -e 'm|\||x' /|/x; $ perl -MO=Deparse -e 'm|\\||x' /\\/ | 'x'; $ perl -MO=Deparse -e 'm|\\\||x' /\\|/x;

    None of those give a regex of /\|/x. You have to go for something different having the same meaning:

    $ perl -MO=Deparse -e 'm|[\|]|' /[|]/;

    You have to use '\' to escape the delimiter and you have to use '\' to escape the '|' regex metacharacter. But Perl provides no way for you to use '\' to both escape the delimiter nature of '|' and also escape the regex metacharacter nature of '|'.

    - tye        

      Thanks tye.

      Sorry I wasn't clear. My original regex did include '/'s in both sides of it. My simplified regex (which I posted) did not.

      I take your points and thanks for your efforts.

      Thanks.
      tel2

Re: Regex delimiters: '/' vs '|' ( m// or s/// or m{} or s{}{} )
by Anonymous Monk on Oct 03, 2014 at 03:03 UTC
      OK - thanks Anonymous Monk!