Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Regex delimiters: '/' vs '|'

by tel2 (Pilgrim)
on Oct 03, 2014 at 02:44 UTC ( [id://1102693]=perlquestion: print w/replies, xml ) Need Help??

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1102693]
Approved by Athanasius
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found