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        


In reply to Re: Regex delimiters: '/' vs '|' (escaped vs escaped) by tye
in thread Regex delimiters: '/' vs '|' by tel2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.