Regex4 – Subexpressions

Alternatives are defined with the binary OR metacharacter, the pipe |

This will find either bob or sam:

Subexpressions can be defined with parenthesis metacharacters to limit scope

This will find submarine and subclipse:

egrep ‘sub(marine|clipse)’ test.txt

Regex1 – The beginnings

Start and end of lines:

^ to indicate the start of a line. This finds all lines beginning with ‘dog’

egrep ‘^dog’ file.txt

This will find lines beginning dog, dogbert, doggydogdog

And just so you know, the ‘^’ is a regular expression ‘metacharacter’, while ‘dog’ consists of ‘literal’ characters.

$ indicates the end of the line, so this will find lines only consisting of ‘dog’:

egrep ‘^dog$’ file.txt

Good eh?