Chapter 13 Regular Expressions
In the world of string matching, regular expressions are the best thing since sliced bread. Admittedly, in the somewhat odd world of string matching, they didn’t really have bread in the first place, but still.
Regular expressions (or “regexes”) is a declarative2 language to describe strings of characters (sequences of letters, punctuation, words, sentences, etc). For example, this is a relatively simple regular expression from http://regexlib.com/Search.aspx?k=email to match email addresses:
There is an excellent online app at https://regex101.com/ that helps you learn about and test regexes by breaking them down and explaining to you exactly what it does. For this regular expression, it provides this explanation:
Of course, this regular expression does not work for all email addresses. If you studied this broken down explanation, you’ll have realised that, for one, this regular expression only matches email addresses with domain names that have a 2- or 3-letter top level domain (TLD, the characters following the last period in a domain name). Many different regexes exist to match email addresses - for example, this one “works 99.99% of the time”:
So, while the building blocks of regular expressions are relatively simple, things can get out of hand quickly.