Regular Expressions & grep

Regular Expressions

PatternMatches
.Any single character
*Zero or more of the previous
^Start of line
$End of line
[abc]Any one of a, b, c
[^abc]Any character except a, b, c
\{2,4\}2 to 4 repetitions (BRE)
`\`Alternation (BRE)

grep flags: -i ignore case, -v invert, -r recursive, -n line numbers, -c count, -E extended regex, -o only matching part, -w whole word.

$ grep -E '^[0-9]{3}-[0-9]{4}
  

 contacts.txt   # phone numbers
$ grep -rn 'TODO' src/
Exam tip: BRE (basic) requires \{ \} and \|; ERE (extended, grep -E) uses bare { } and |. egrep = grep -E.