{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The Unix Shell: Regular Expressions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Regular Expressions\n", "\n", "A regular expression (regex) is a text **pattern** that can be used for searching and replacing. Regular expressions are similar to Unix wild cards used in globbing, but much more powerful, and can be used to search, replace and validate text.\n", "\n", "Regular expressions are used in many Unix commands such as `find` and `grep`, and also within most programming languages such as R and Python.\n", "\n", "We only show basic usage here to get you started. To get practice, first spend some time at to get a better understanding of how to use regular expressions, then find out how to use them in your text editor to do a search and replace." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Matching characters\n", "\n", "We will practice using `grep`. If a successful match is found, the line of text will be returned; otherwise nothing." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grep --help | head -n 20" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Literal character match" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "echo abcd | grep abcd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep bc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### No match for `ac`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep ac" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Case insensitive match" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep -i A" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matching any single character\n", "\n", "The `.` matches exactly one character." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep a.c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep a..c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep a..d" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Matching a character set" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep [0123456789]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep [0-9]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep [abc]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep [def]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep [a-z]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep [A-Z]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exceptions\n", "\n", "The `^` within a character set says match anything NOT in the set." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep [A-Z]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep [^A-Z]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pre-defined character sets\n", "\n", "Many useful sets of characters (e.g. all digits) have been pre-defined as [character classes](https://www.gnu.org/software/grep/manual/html_node/Character-Classes-and-Bracket-Expressions.html) that you can use in your regular expressions. Character classes are a bit clumsy in the Unix shell, but simpler forms are often used in programming languages (e.g. '\\d' instead of '[:digit:]')." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep ['[:alpha:]']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep ['[:digit:]']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2b | grep ['[:punct:]']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo a2,b | grep ['[:punct:]']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Alternative expressions\n", "\n", "We use the -E argument here to avoid having to escape special characters\n", "```\n", " -E, --extended-regexp\n", " Interpret pattern as an extended regular expression (i.e. force\n", " grep to behave as egrep).'\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo cat | grep -E '(cat|dog)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Without `-E`\n", "\n", "We need to escape the special characters `(`, `|` and `)`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo cat | grep '\\(cat\\|dog\\)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We love dogs as well" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo dog | grep -E '(cat|dog)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### But not foxes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo fox | grep -E '(cat|dog)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Be careful - use of square brackets means something different" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo fox | grep -E '[cat|dog]'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Character set modifiers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Anchors\n", "\n", "`^` indicates start of line and `$` indicates end of line." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep ^ab" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep ab$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep ^cd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abcd | grep cd$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Repeating characters\n", "\n", "- `+` matches one or more of the preceding character set\n", "- '*' matches zero or more of the preceding character set\n", "- '{m, n}' matches between m and n repeats of the preceding character set." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abbbcd | grep abcd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abbbcd | grep -E ab+cd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abbbcd | grep -E ab*cd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abbbcd | grep -E 'ab{1,5}cd'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo abbbcd | grep -E a[bc]+d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matching words with word boundaries\n", "\n", "`\\<` and `\\>` indicate word boundaries. That is, `\\` will only match `foo bar` or `bar foo` but not `foobar` or `barfoo`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo 'other ones go together' | grep 'the'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo 'other ones go together' | grep '\\'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo 'other ones go together' | grep '\\'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Capture groups and back references" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo \"123_456_123_456\" | grep -E '([0-9]+).*\\1'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo \"123_456_123_456\" | grep -E '([0-9]+)_([0-9]+)_\\1_\\2'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "echo \"123_456_123_123\" | grep -E '([0-9]+)_([0-9]+)_\\1_\\2'" ] } ], "metadata": { "kernelspec": { "display_name": "Bash", "language": "bash", "name": "bash" }, "language_info": { "codemirror_mode": "shell", "file_extension": ".sh", "mimetype": "text/x-sh", "name": "bash" } }, "nbformat": 4, "nbformat_minor": 2 }