What we covered in the previous post are so-called basic regular expressions. There are two things, for those of you who are interested, to look into (alongside references for each):
POSIX character classes (“POSIX Bracket Expressions,” n.d.)
Extended regular expressions (“Understanding Regular Expressions,” n.d.) (this reference covers what is covered in this article, as well as extended regular expressions)
I have personally never used regular expressions in grep because I usually want to find out if a file contains some particular word. However, I have used regular expressions in programming languages (like Python) to clean up my input, so I think you learned something useful because you will be able to use the basic regular expressions if the need arises and you have gotten an introduction to something you will probably use from time to time if you decide to write computer programs.
Regular expressions are symbolic notations used to identify patterns in text. (Shotts, 2019)
The more complex explanation of what regular expressions are goes into theoretical computer science (more specifically, automata theory) and is way out of scope for this post (and I would have to review the stuff I learned in my Introduction to the theory of computation course). But, what I will say is just this – people have found some clever mathematical ways of describing patterns in text. In order to find out more about the development of the idea of regular expressions, have a look at the Wikipedia history entry here: (“Regular expression,” n.d.)
So, regular expressions help you find patterns in text. That’s their usage.
The time has come to become acquainted with regular expressions. To repeat, regular expressions allow us to match patterns in text. (Shotts, 2019) It is important to note that regular expressions differ from shell globbing (wildcards), since shell globbing is related to the shell and regular expressions are used to match patterns in text on a much broader level (regular expressions are used in programming languages, for example, while wildcards are only used in the shell). More explanations can be found in (“Regular expressions VS Filename globbing,” n.d.) and (“Globbing and Regex: So Similar, So Different,” n.d.)
Let’s first list the special characters in regular expressions, then show them applied to a couple of examples:
Any character is denoted by .
* denotes zero or more characters; a* means zero or more characters a (“Basic Regular Expressions: Kleene Star,” n.d.)
Anchors are denoted by ^ and $; they denote beginning and the end of the string pattern we are matching, respectively
Bracket expressions are denoted with  – if ^ is the first character in the bracket expression, we treat it as a negation (meaning match everything except the thing in the bracket expression); if ^ is not the 1st character in the bracket expansion, it is matched literally
- denotes a range in a bracket expression; if it is the first character, it is matched literally, if not, then it denotes a range; you can have multiple ranges (as in [A-Za-z] if you wanted to capture all the letters)
Let’s look at a couple of examples. We will use grep, because grep’s name is actually globally search a regular expression and print (“grep,” n.d.). So grep was actually about regular expressions all along! If this was a mafia movie, grep would now get shot and thrown in the sea by the docks. Anyway, let’s get back to our examples:
[A-Z] says find any line that “has letters A to Z in it”. The second grep call (with the regular expression [AI])says “find any line that has letters A or I in it”. [^A-Z] says find any line that “has a character which is not A to Z in it”. This prints every line, since every line contains lowercase letters. However, the regular expression ^[^A-Z] says “anything that does not begin with A to Z”. Since every line begins with an uppercase letter, I get no output.
I just wanted to note that it is vital to enclose regular expressions in quotes. Prefer single quotes over double quotes; take my word for it now, you will see the difference between those types of quotes later on. (“What’s the Difference Between Single and Double Quotes in the Bash Shell?,” n.d.)). Otherwise, expansion can occur in a place where you meant to pass a regular expression, because expansion occurs before the command is executed. Just remember this, but for an example refer to (“Globbing and Regex: So Similar, So Different,” n.d.).