Dariusz
Dariusz

Reputation: 22291

Does a dot have to be escaped in a character class (square brackets) of a regular expression?

A dot . in a regular expression matches any single character. In order for regex to match a dot, the dot has to be escaped: \.

It has been pointed out to me that inside square brackets [] a dot does not have to be escaped. For example, the expression: [.]{3} would match ... string.

Doesn't it, really? And if so, is it true for all regex standards?

Upvotes: 173

Views: 50355

Answers (1)

lilactiger89
lilactiger89

Reputation: 2518

In a character class (square brackets) any character except ^, -, ] or \ is a literal.

This website is a brilliant reference and has lots of info on the nuances of different regex flavours. http://www.regular-expressions.info/refcharclass.html

Upvotes: 228

Related Questions