Reputation:
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?
Upvotes: 10
Views: 21485
Reputation: 3211
Yep, semicolon is not a meta character, so I guess it doesn't need to be escaped.
Upvotes: 0
Reputation: 52994
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.
Upvotes: 5
Reputation: 496852
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.
Upvotes: 3
Reputation: 118128
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
.
Upvotes: 14