The Personal Web Pages of Chris X. Edwards
Regular Expression Tutorial
When defining character classes using brackets, it is possible to encounter problems because of the character class metacharacters. These problems basically comes in three categories.
There are a couple of ways to cure this problem. The first is to use a \ (backslash) to let the interpreter know that you mean a literal right bracket.
The other solution is to reorder things a bit so that it is less ambiguous. If you have a character class of ], the interpreter is smart enough to realize that since you didn't specify anything by the first closing bracket, that maybe you meant for the bracket itself to be included. It then continues to look for another closing bracket which it treats as the metacharacter. So here's the grouping characters put in a correct character class.
Note that including another left bracket, [, in the class creates no special confusion. It is impossible and illogical to have nested character classes. Once you're in the class definition, left brackets are treated literally.
What if your regular expression just tries to find a single left bracket? If you specify a regular expression such as this:
You will not find "Note " and "note " as you might expect and hope. It will, however, find "Note 4]" if it can. Again there are two ways to handle this:
Note that in both cases, the last right bracket, "]" is a literal.
This character class is not negated. It is any of the specified characters including the "^". If you don't like to rearrange things, you can also make the "^" a literal with a backslash.
This should find only a math operator but produces an invalid range. Here are two ways that work.
What if you want a range from the backslash to something? What if you wanted these characters "\", "]", "^", "_", and "`". In theory, because they are listed in their conventional order you could specify a range from the "\" character to the "`". But "[\-`]" would not work that way. If you really really wanted to do something stupid like this, this would work "[\\-`]".
This page was created with only free, open-source, publicly licensed software.|
This page was designed to be viewed with any browser on any system.
|Chris X. Edwards ~ December 2003|