1. regex (Regular Expressions)

Regular expressions are a powerful tool to do pattern based selection. It uses special notations similar to those we've encountered already such as the * wildcard.

We'll go through a couple of the most common regular expressions, these are almost universal with any programming language.

Well use this phrase as our test string:

sally sells seashells
by the seashore

1. Beginning of a line with ^

would match the line "by the seashore"

2. End of a line with $

would match the line "by the seashore"

3. Matching any single character with .

would match by

4. Bracket notation with []

This can be a little tricky, brackets allow us to specify characters found within the bracket.

would match: dig, dog, dug

The previous anchor tag ^ when used in a bracket means anything except the characters within the bracket.

would match: dog and dug but not dig

Brackets can also use ranges to increase the amount of characters you want to use.

will match patterns like dag, dbg, and dcg

Be careful though as brackets are case sensitive:

will match dAg, dBg and dCg but not dag, dbg and dcg

And those are some basic regular expressions.


Try to combine regular expressions with grep and search through some files.

grep [regular expression here] [file]


What regular expression would you use to match a single character?