views:

963

answers:

6

Someone is telling me I need to escape a semicolon in a Perl regular expression literal. That is, to match a line containing a semicolon, I should use /\;/ and not /;/.

From what I've read, the semicolon has no special meaning in a regular expression literal, so escaping it seems unnecessary. I've done some experiments and /;/ seems to work fine. With warnings turned on and the use strict; pragma in effect, perl doesn't complain.

Is there some reason why /\;/ is better than /;/? Is this version-dependent?

+1  A: 

there is no need to escape it.

ghostdog74
+7  A: 

There is absolutely no need to escape a semicolon in a regular expression pattern. There has not been such a need in the almost ten years I have used Perl and I doubt there ever was.

A concise summary of special characters and escape sequences can be found in perldoc perlreref.

Sinan Ünür
+1  A: 

No. /;/ should always work just fine.

innaM
+3  A: 

Perhaps this is a habit developed from using perl one-liners on the command-line and not quoting, so the ';' split the rest off into another command? Anyway, like everyone else says, no need.

Jefromi
@Jefromi Hmmmm ... That sounds like a good guess. I was lost as to why anyone would think there was such a need.
Sinan Ünür
+3  A: 

Perhaps someone thinks that the semicolon needs escaping because their editor's syntax highlighting gets confused by the embedded semicolon. In my experience, most editors have a lot of trouble coping with Perl's syntax. Remember, Only perl can parse Perl.

Adam Batkin
@Adam Another good guess.
Sinan Ünür
OTOH, don't forget about http://search.cpan.org/perldoc/PPI
Sinan Ünür
Wow, PPI looks awesome. And it is a great example of well-written documentation, including a good description of why it is so difficult to parse perl.
Adam Batkin
A: 

Yep, semicolon is not a meta character, so I guess it doesn't need to be escaped.

jeje
@jeje Why guess?
Sinan Ünür
@sinan Cause I didn't check before answering :)
jeje