# CS2121 lab exercise 1 - hint

The use of "[ ]" to mean "any character in the list", and "-" to simplify writing the lists, is described here.

The use of "[^ ]" to mean "any character except those in the list", is described here.

Read on to find out what you have to do to include any of the characters "[", "]", "-" or "^" in your list:

If "-" is actually one of the characters you want to include in your list, you must put it either at the start or the end, otherwise it will be treated as meaning a range of characters.

For example, suppose you wanted to recognise the arithmetic operators: "+", "-", "*" or "/". If you write:

```[+-*/]
```
what you will actually get is: any of the characters between "+" and "*", or a "/".
To get what you really want, you have to write:
```[-+*/]
```
or:
```[+*/-]
```

What about "[" and "]" ? We can unambiguously use "[" e.g.:
```[#+[*]
```
as, between "[" and "]", another "[" doesn't have a special meaning.

However, if we try to use "]" like this:

```[#+]*]
```
then egrep thinks it has found the end of the list at the first "]", and can get very confused by the following characters (i.e. "*" and "]" in this example). Instead, we have to put the "]" at the start of the list, like this:
```[]#+*]
```
The authors of egrep assume that no-one would be stupid enough to have a completely empty list (i.e. "[]") and take the first "]" as an ordinary character, and look for another "]" to terminate the list properly.
If "^" is actually one of the characters you want to recognise in a list, you must not put it at the start - if you do, it will have its special meaning - just put it anywhere else in the list!

For example, suppose you wanted to recognise the special characters: "^" or "&" or "*". If you write:

```[^&*]
```
what you will actually get is: any character except "&" or "*"
To get what you really want, you just have to write:
```[&^*]
```
or:
```[&*^]
```

All other characters used inside "[ ]" just mean themselves - e.g. an "a" means "recognise an a" etc. This even includes characters like "*" and "+" and "|", which have special meanings if they are used anywhere else.