Yes. If the alphabet is ASCII, then the character '#' is matched by [^0-9]
|
Regular Expression (Delimit with quotes) |
|
|
String (Delimit with quotes) |
|
Rule 5. Zero or One Instance
When a character is followed by ? in a regular expression
it means to match zero or one instance of the character.
So X? matches an 'X' if there is one in the string,
but otherwise matches nothing.
| Regular Expression | Matches | |
|---|---|---|
a?bc | abc | bc |
colou?r | color | colour |
computers? | computer | computers |
A? | A | Ø |
The Ø in the above table means that the regular expression A?
matches the empty string — the string that contains no characters.
? can be used following a range of characters or an exclusion range,
in which case it means to optionally match one or zero characters from that range.
| Regular Expression | Matches | ||
|---|---|---|---|
[a-z]?[1-9] | a1 | 1 | z9 |
WX[^AB]? | WX | WXC | WXY |
[0-9]?[0-9]?[1-9] | 007 | 05 | 9 |
Which of the following strings match [BD][ae][ae]?d ?
| String | Bed | Bead | Dead | aed | Ba?d | Ded | Bea |
|---|---|---|---|---|---|---|---|
| Match or Reject? |
Write a RE that matches the strings Ba?d , Be?d , De?d , and Da?d (these are strings, not patterns, and the question mark is part of the string).