Why is only ) a special character and not } or ]?

前端 未结 4 1110
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 05:47

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

4条回答
  •  清酒与你
    2021-01-19 05:59

    Every where in a regular expression, regardless of engine and its standards, a parenthesis should be escaped to mean a literal character. Even the closing parenthesis. However, it doesn't apply to POSIX regular expressions:

    ) The shall be special when matched with a preceding , both outside a bracket expression.

    But the interesting part is that POSIX has a separate definition for a right-parenthesis for times it should be treated as a special character. It doesn't have it for } or ].

    Why other engines don't follow this rule?

    Call it implementation peculiarities or historical reasons that have something to do with Perl as commented in PCRE source code:

    /* It appears that Perl allows any characters whatsoever, other than
    a closing parenthesis, to appear in arguments, so we no longer insist on
    letters, digits, and underscores. */
    

    It seems that with all that special clusters in more advanced engines treating a closing parenthesis as a special character will cost much less than implementing POSIX standard.

提交回复
热议问题