in reply to Regex delimiters: '/' vs '|'
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex delimiters: '/' vs '|' (escaped vs escaped)
by tel2 (Pilgrim) on Oct 03, 2014 at 03:54 UTC |