0

In here we can find:

Because we want to do more than simply search for literal pieces of text, we need to reserve certain characters for special use. In the regex flavors discussed in this tutorial, there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {, These special characters are often called "metacharacters". Most of them are errors when used alone.

Why opening curly brace can be escaped while closing cannot? It has regex specific meaning after all.

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
  • 4
    Your statement that a closing curly brace cannot be escaped is not true. There is nothing wrong with writing `\}` instead of `}` other than using an unnescessary escape. It however is not needed to ever escape `}` as explained in L3viathan's answer. – Sebastian Proske Jan 15 '19 at 13:38
  • related: https://stackoverflow.com/q/52431667/1048572 – Bergi Jan 15 '19 at 14:44

2 Answers2

1

Because it is not needed. Whereever a closing brace has a special meaning, a literal closing brace can't occur anyways:

foo{2,3} matches "fooo" and "foooo", whereas foo\{2,3} matches "foo{2,3}".

What would foo{2,3\} mean? Inside a quantifier started by {, only numbers and a comma can occur.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
1

To be more precise, it is saying:

opening parenthesis ( ... the opening square bracket [, and the opening curly brace { ... are errors when used alone.

and it's right because when most engines encounter an opening bracket while walking through a regex it means something more than a literal character to them so they try to look for the closing pair otherwise it is a syntax error. Until now they do not parse a regex from the end to throw an error for unpaired closing bracket.

revo
  • 47,783
  • 14
  • 74
  • 117