Regular Expressions
A regular expression, also called a regex (plural regexes), is a kind of pattern-matching syntax, similar to wildcards, but much more powerful. A complete description of regexes would fill many chapters, so we will restrict ourselves to a reasonable subset in this chapter.
The most common use case of regexes is with the grep
and sed
commands, which we studied in the previous chapter. The basic operation we perform with a regex is to match it against some text:
grep
can search for text matching a regexsed
can search and replace the text matching a regex with a specified replacement stringNote
Since the special characters in regex syntax overlap with those that the shell uses, always pass regexes in single quotes to ensure that the shell passes them literally to the command, without interpretation. Commands that accept regexes will handle escape sequences by themselves.
There are two kinds of regexes, that is, basic and extended...