CS2121 lab exercise 1 - hint
If you need to match several characters
- it may be that you know how exactly many characters to match
- it may be that you have to match any number of characters,
depending on how many are present, or
- it may be that there might not even be any characters there to match!
In case 1, for small numbers, the simplest thing is usually to write
out the pattern the correct number of times - e.g. to recognise course-unit
numbers, that look like "CS1011" or "MT1662", we could write:
[A-Z][A-Z][0-9][0-9][0-9][0-9]
Some versions of regular expressions make life simpler e.g.:
[A-Z]{2}[0-9]{4}
In case 2, suppose we want to recognise numbers consisting of any number of
digits, we can use "+", which means "one or more of", to write e.g.:
[0-9]+
In case 3, suppose we want to recognise decimal numbers, and allow there to be
no digits before the decimal point (e.g., so we can recognise both "0.2" and
".2"). We can use "*", which means "none or one or more of", to write e.g.:
[0-9]*\.[0-9]+
(I had to use a "\" before the "." for the same reason as in
part a6.)