I\'m reading Jan Goyvaerts\' \"Regular Expressions: The Complete Tutorial and Reference\" to touch up on my Regex.
In the second chapter, Jan has a section on \"spec
The following paragraphs give an answer. I'm citing from Jan's website, not from the book, though:
If you forget to escape a special character where its use is not allowed, such as in
+1
, then you will get an error message.Most regular expression flavors treat the brace
{
as a literal character, unless it is part of a repetition operator likea{1,3}
. So you generally do not need to escape it with a backslash, though you can do so if you want. But there are a few exceptions. Java requires literal opening braces to be escaped. Boost and std::regex require all literal braces to be escaped.
]
is a literal outside character classes. Different rules apply inside character classes. Those are discussed in the topic about character classes. Again, there are exceptions. std::regex and Ruby require closing square brackets to be escaped even outside character classes.
It seems like he uses "needs to be escaped" as his definition for "special character", and unlike )
, the ]
and }
characters need not be escaped in most flavours.
That said, you wouldn't be wrong calling them special characters as well. It's definitely a best practice to always escape them, and in no flavour \]
and \}
mean anything else than a literal ]
or }
.
On the other hand, they have their special meaning only inside a specific (parsing) context, namely when they follow [
and {
respectively. There are similar cases: :=> all have a non-literal meaning inside a specific context, and we wouldn't normally call these "special characters" either.
And while we could say the same about )
, almost no flavour allows for it to occur on its own outside of groups, because pairs of parentheses always need to match. Its only usage is in the special context, and therefore )
is considered a special character.