Vim: Regex Patterns
Regular expressions are an insanely useful tool. It is absolutely worth the effort to learn how to use them, even a modicum of competence with regular expressions will pay dividends for your entire career. When you come across a problem that fits well with the use regular expressions it will save you hours and hours of work (not to mention how satisfying it is solve a gnarly problem with a few lines of code). The challenge is that without some mastery you don’t always spot the opportunities.
The more you understand regular expressions, the more leverage you will gain from the text processing tools (Vim, Sed, Awk, and Grep in particular), although regular expression support is included in many more tools. Most modern languages have great inbuilt support for regular expressions as well.
There are many implementations of regular expressions. This works out mostly ok, you know the tools you regularly use well and most regex implementations cover the basics well so it’s only the more esoteric features that may or may not be available in your current tool (or more frustratingly, work in s slightly different way). In practice, I have not found this to be a big deal.
The Basics
Meta-characters:
^
start of line$
end of line.
a period matches any character|
is alternatives(
and)
are used to group multiple characters and to limit the scope of alternaltives?
indicates optionality+
indicates one or more*
indicates zero or more{4,7}
is the interval quantifier, this will match what precedes it between 4 and 7 times, not always supported
Character Class
A character class is the set of characters allowed at that particular point. Only one character is used.
[
and]
delineates the character class[ae]
will match the letter e or the letter a[a-z]
a dash is used to indicate a range, in this case all lowercase letters[a-zA-Z]
multiple ranges are ok, this is all lowercase and uppercase latters[0-9$]
ranges can be combined with literals, in this case any number or the dollar sign- Note:
$
and^
are not treated as meta-characters inside a character class
Back Referencing
Escaping Meta-characters
Resources
Master Regular Expressions, 3ed.,