user147152
user147152

Reputation:

Do I need to escape a semicolon in a Perl regular expression literal?

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

Answers (6)

jeje
jeje

Reputation: 3211

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

Upvotes: 0

Adam Batkin
Adam Batkin

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

Cascabel
Cascabel

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

innaM
innaM

Reputation: 47829

No. /;/ should always work just fine.

Upvotes: 1

Sinan Ünür
Sinan Ünür

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

ghostdog74
ghostdog74

Reputation: 342333

there is no need to escape it.

Upvotes: 1

Related Questions