Language | Example Strings | Regular Expression | ||
---|---|---|---|---|
Any number of A's or any number of B's | AAA | BBBB | Ø | A*|B* |
One or more A's or any single character not an A | AAA | C | x | A+|[^A] |
An integer or a decimal fraction | 143 | 834.91 | 89.0 | [0-9]+|[0-9]+\.[0-9]+ |
Regular Expression (Delimit with quotes) |
|
String (Delimit with quotes) |
|
Rule 11: Parentheses
Parentheses are used to group subexpressions.
A subexpression inside parentheses must be completely matched.
For example,
(cat|snake) eyes
matches
cat eyes or snake eyes.
Parentheses have high precidence, so that what lies between them is a complete
subexpression. (cat|snake) eyes
means
cat or snake, followed by eyes.
Regular Expression | Matches | Does Not Match |
---|---|---|
puppy|ies | "puppy" or "ies" | "puppies" |
pupp(y|ies) | "puppy" or "puppies" | "ies" |
puppy* | "pupp", "puppy", "puppyy", "puppyyy", . . . | "puppypuppy" |
(puppy)* | Ø, "puppy", "puppypuppy", "puppypuppypuppy", . . . | "ppyyyy" |
(puppy)+ | "puppy", "puppypuppy", "puppypuppypuppy", . . . | Ø, "pupp" |
(puppy)? | Ø, "puppy" | "puppypuppy" |
(dog |cat |bat )+ | "dog ", "dog cat dog ", "bat bat bat " | "dog", "dogcatbat" |
The *
, +
, and ?
quantifiers apply to the single character to their left,
or to the parenthetical subexpression to their left.
Write a regular expression to match:
"porkchop", "lambchop", "Porkchop", "Lambchop", "porkchops", "lambchops", "Porkchops", or "Lambchops".