In Regular Expressions Quick Start, it reads

Twelve characters have special meanings in regular expressions: 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.

In its specification, (), [,{ are metacharacters whereas 'closing sqare bracket' and 'closing curly brace' are not.

Obviously,[ and { unable to take an effect individually just like opening parenthesis ( should partners ).

What's the reason that causes ] and } failing to be selected?


You can write a plain ] in most regex engines and have it match a ] in the input. This works because ] on its own does not have special meaning.

Contrariwise, you can't write a plain [ to match a [ in the input - the regex engine will complain that the brackets are unbalanced. This is probably why this text says that [ is special and ]isn't.

This seems unintuitive, but it's true. ] has a special meaning only within a character class, and the special meaning is "terminate the class". This is just like -, which also has a special meaning only within a character class (the meaning is "create a range between the two neighbouring letters"). You wouldn't take this to mean that - in general is a metacharacter, and for the same reason ] in general isn't a metacharacter.

If this seems weird, you're in good company, but it is the most logical way of looking at it. The reason it looks weird is that we're conditioned to consider [] () {} as matching pairs. (Compare the well-known "paradox" that ())(is a palindrome, but (()) isn't.)

  • It seems odd therefore that ) doesn't fit into the same category as }, ] and -. Is there a reason for this inconsistency? – David Arno Dec 7 '17 at 10:21
  • @DavidArno You're right, most regex engines complain about a plain ). I never noticed that, probably because you rarely get tings starting with ). By my logic they should be able to deal with it, since it's unambiguously a normal character. I'm not sure why alternative-processing parsers tend to be pickier than the character-class parsers. Maybe it's because character-class logic is a lot simpler and more straightforward to implement? – Kilian Foth Dec 7 '17 at 10:36
  • 1
    I guess the reason behind it might have been an attempt to make regex expressions less error-prone (the irony of that statement isn't lost on me, BTW). [] and {} tend to only contain a few characters between them, so mistakes can be easier to spot. () can be nested and contain large sections of an expression, so spotting erroneous )'s could be more difficult. Pure speculation though. – David Arno Dec 7 '17 at 10:59
  • 1
    Actually according to the POSIX spec: "The close-parenthesis shall be considered special in this context only if matched with a preceding open-parenthesis." But obviously some regex engines consider it a special character regardless. Anyway it's not like the spec came first - regex syntax has been around and morphed over time, and started as very simple, and wasn't consistent across engines... so parenthesis handling is probably different just due to that. – hadriel Dec 8 '17 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.