Regular Expressions I
A fast introduction to regular expressions.
There are many good online resources for learning regular expressions. If you are unhappy with one, search for another.
Essentials to know:
- Grouping:
( ... )
makes a group. The parentheses do not appear in the item you are matching. - Choices:
(A|B)
matches either A or B. - Repeat the last item:
?
means 0 or 1;*
means 0 or more;+
means 1 or more. - Character classes:
[abc]
matches one of a, b, or c.[abc]+
matches aba, cba, and abcbca. Any string of length 1 or more made up of the letters a, b, and/or c.[a-z]
matches any letter from a through z.
Notes
- The convention is to use lowercase for a kind of character (like
digit is
\d
) and uppercase for not that kind of character (non-digits would match\D
).
Exercises
Match with a regular expression, or say what patterns will match the given regex.
-
MMMM…. NNN….
-
MMMM….andM…..
-
Give examples of what this recognizes:
M*[and]M*
M*[and]+M*
-
(AB)+C
-
(Mary had a little (sheep|lamb|goat))+
-
( [a-z] \+= 1; ))+
-
(statement)*
-
Write a regexp to match positive integers.
-
A variable name can have lower case and upper case letters. It may include digits and underscores. A variable name cannot start with a number.