Regular Expressions

Videos

Note: if you want quick notes from a video to review what was discussed, just check out the "about" section on the YouTube page.

Exercises

  • Make an alias in your shell's configuration file so that you always use grep -P --color.  Note that this will only work on Linux, not on Macs.
  • Checkout a copy of samking-config-files.  We'll be grepping over them.
  • Search for CUSTOMIZE in any of the files.  Try doing this without using a *.
  • Search for all lines including 'alias ls' in zshrc
  • Search for all lines that are comments -- all lines that start with a #
  • Search for all lines that have inline comments -- all lines that start with something other than a #, but contain a # somewhere in the line.
  • Search for all lines that have digits.
  • Search for all lines that are entirely whitespace.
  • Search for all lines that have no whitespace.
  • Search for all lines over 80 characters long.
  • Search for all lines whose last character is a single quote or double quote (all lines that end with strings).
  • Search for all lines with 80 #s.  Those lines are used for section delimiters.
  • Search the vimrc for all lines with either set or map.
  • Search the vimrc for all lines where either set or map is a whole word (eg, don't include noremap or setlocal).  Use a group so that you don't have to duplicate your word boundary characters.
  • Search the vimrc for all lines where the same character is repeated twice
  • Search the vimrc for all lines that meet the following conditions: the line is greater than 80 characters, and the line has a double quote somewhere in it (the comment character in a vimrc).  Note that the quote doesn't have to be at the start, so you can't just search for a quote followed by 79 characters.
  • What is the difference between a lazy and a greedy regular expression?  Why doesn't the difference matter much in grep?  You should be able to see the difference easier if you use the --color or -o (which will only print the match) flags.

Try out Regular Expression Crosswords and Regular Expression Golf.

If you have extra time, check out some extra exercises from an old version of this course.