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

流过昼夜 提交于 2019-11-26 06:00:48

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/19976018/does-a-dot-have-to-be-escaped-in-a-character-class-square-brackets-of-a-regula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!