CSE 391
Regular Expressions�grep
Slides created by Josh Ervin and Hunter Schafer. �Based off slides made by Marty Stepp, Jessica Miller, Ruth Anderson, Brett Wortzman, and Zorah Fung
ROADMAP
AGENDA
REGULAR EXPRESSIONS
SIMPLE REGEX
grep -E “Chocolate” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -Ei “Chocolate” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -E “a” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -Ei “a” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -E “ar” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -E “.a” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -E “^K” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -E “\<T” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -E “t\>” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
SIMPLE REGEX
grep -E “\.” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat.
Dark chocolate
GLOSSARY
Syntax | Functionality |
. | Any character |
^ | Start of line |
$ | End of line |
\< | Start of word |
\> | End of word |
\ | Escape the following character |
-i | (Flag to grep) match case insensitively |
Syntax | Functionality |
. | Any character |
^ | Start of line |
$ | End of line |
\< | Start of word |
\> | End of word |
candies.txt
Twix
Sweet Tart
Chocolate
Almond Joy
Jolly Ranchers
Kit Kats
Dark chocolate
Suppose we have the file candies.txt on the left. What is the full grep command to print out all lines that contain four-letter words that start with the letter T (Uppercase)?
REGEX
grep -E “Twix|Tarts” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat
Dark chocolate
REGEX
grep -E “(e|a)t” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat
Dark chocolate
Reeses
REGEX
grep -E “e*t” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat
Dark chocolate
Reeses
REGEX
grep -E “e+t” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat
Dark chocolate
Reeses
REGEX
grep -E “r?t” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat
Dark chocolate
Reeses
REGEX
grep -E “(es)+” candies.txt
candies.txt
Twix
Sweet Tarts
Chocolate
Almond Joy
Jolly Ranchers
Kit Kat
Dark chocolate
Reeses
GLOSSARY
Syntax | Functionality |
| | Logical or |
* | Zero or more of |
+ | One or more of |
? | Zero or one of |
() | Group characters together |
Syntax | Functionality |
| | Logical or |
* | Zero or more |
+ | One or more |
? | Zero or one |
() | Group characters |
kitkats.txt
KitKats
Kit Kats
Kit kat
kitkats
Kit Kats
kitkat
kitkats
KITKATS
Suppose we have the file candies.txt on the left. We want to print out all lines containing kitkats. All of the following criteria are valid:
REGEX
grep -E “[abc]” passwords.txt
passwords.txt
password123
supersecret
NoHackingPlz
122password
Not a valid password.
won’t remember
REGEX
grep -E “[a-z]” passwords.txt
passwords.txt
password123
supersecret
NoHackingPlz
122password
Not a valid password.
won’t remember
REGEX
grep -E “[.]” passwords.txt
passwords.txt
password123
supersecret
NoHackingPlz
122password
Not a valid password.
won’t remember
REGEX
grep -E “[^ao]” passwords.txt
passwords.txt
password123
supersecret
NoHackingPlz
122password
Not a valid password.
won’t remember
REGEX
grep -E “[0-9]{2}” passwords.txt
passwords.txt
password123
supersecret
NoHackingPlz
122password
Not a valid password.
won’t remember
REGEX
grep -E “(..)\1” passwords.txt
passwords.txt
password123
supersecret
NoHackingPlz
122password
Not a valid password.
won’t remember
GLOSSARY
Syntax | Functionality |
[ ] | Character set |
[^ ] | Negate character set |
[a-z] | All lowercase characters |
[A-Z] | All uppercase characters |
[0-9] | All digits |
\1 | Back reference earlier character |