What are regular expressions?

Although the concept of regular expressions (often abbreviated to regexp or regex) is quite simple, it's not that easy to describe what regular expressions are. In a nutshell, a regular expression is a way of describing a pattern which a computer uses to match strings of text.

Regular expressions are being used for searching or manipulating texts. You can use regular expressions in many computer programs, like text editors and command-line tools (for example grep, sed and awk), but also a lot of programming and scripting languages support the use of regular expressions.

You are probably familiar with the wildcard character for searching files and directories in your operating system. For example, "*.txt" will result in any file that ends with ".txt". A regular expression is something like the "*.txt", but it's a lot more powerful than the wildcard possibilities in your file system.

Regular expressions can contain two types of characters. Literal characters (for example: txt will match "txt") and metacharacters like the asterisk (*) when searching for the "*.txt". The magic of regular expressions is mainly about the power of those metacharacters. Note: in regular expressions an asterisk is not a wildcard, but a quantifier meaning "zero or more".

When should you use them?

The possibilities are endless I guess, as described in this famous comic from xkcd...

xkcd comic about regular expressions
Created by Ruud Jansen.