As a Linux user or administrator, the topic of regular expressions probably comes up fairly often, and if it doesn’t, you’re missing out. A lot of command-line programs like grep or scripts in perl, python, or PHP scripts, make use of, or can make use of, regular expressions.
So what is a regular expression?
Here is a list of basic perl-based regular expressions and what they do:
- . Matches any characters
- * Matches 0 or more of the preceding character
- + Matches 1 or more of the preceding characters
- ? Matches 0 or 1 occurrences of the preceding character (the preceding character is optional)
- \d Matches a single digit (‘[:digit:]’ in POSIX)
- \w Matches any word character (including alphanumeric and underscore; ‘[:word:]’ in POSIX)
- [ABC] Matches any single character from the class (i.e. ‘A’ or ‘B’ or ‘C’)
- [ABC]+ Matches 1 or more characters from the class
- $ Matches the end of the string
- ^ Matches the beginning of the string
- | Matches on the expression either before or after ‘|’
It’s not pretty, and as a result, regular expressions can become very messy looking and difficult to grasp. However, they are very powerful. Here are some examples of regular expressions in actions:
The above matches either “foo” or “bar”.
The above matches either https://www.foo.com, https://foo.com, http://foo.com, or http://www.foo.com.
The above matches “foo”, “boo”, or “oo”.
The above matches “foo”, “boo”, “fboo”, “ffoo”, and so on, but not “oo”.
Knowing regular expressions is, obviously, most useful when programming, however it can be very useful for command-line tools as well. Grep, when called as egrep can use POSIX regular expressions which elevates grep to a whole new level of convenience. The find command also supports using regular expressions to find files, and likewise the awk and sed tools support regular expressions.
For system administrators, many programs can use regular expressions in configuration files, such as Apache. The “*Match” directives in Apache (i.e., <DirectoryMatch> and <RedirectMatch>) support regular expressions, as do rewrite rules.
Knowing regular expressions isn’t for the “elite” or even just for sysadmins; even Google supports regular expressions in search queries! No, they are useful for many people, even if you just learn basic regular expressions such as those noted above. They make searching for things so much easier, and can reduce multiple commands or directives down to a single one, which can ultimately enhance productivity.