Intro
- Regular Expressions = "regex" or "regexp"
- Comes from the
ed
editor but you'll mostly encounter thegrep
program - A quick way of describing a particular pattern of characters in text
- Allows for extremely effective search and replace
- Comes from the
ed
editor from UNIX but you'll mostly encounter thegrep
program
Knowing [regular expressions] can mean the difference between solving a problem in 3 steps and solving it in 3,000 steps. When you’re a nerd, you forget that the problems you solve with a couple keystrokes can take other people days of tedious, error-prone work to slog through.
– Cory Doctorow in Here's what ICT should really teach kids: how to do regular expressions
Notes
BRE vs ERE
grep
uses so called "basic regular expressions" (BRE) by default.- What we normally consider "regular expressions" are actually "extended regular expressions" (ERE)
- The main difference is in backslashing the meta characters:
- BRE:
\?
,\+
,\{
,\}
- ERE:
?
,+
,{
,}
- BRE:
- EREs can be turned on in
grep
by passing the-E
parameter. - More details here
Links
- The origins of grep: Brian Kernighan, one of the forefathers of UNIX discusses how
grep
came to be, and it makes for a rather interesting story! (If you are in a hurry, here is a 10 minute video.) - RegexOne: interactive tutorial.
- Regex101: an integrated development environment (IDE) for regular expressions.
- Regexper: visualises regular expressions as "proto programs".
- Games:
- There are also some alternatives to
grep
, esp.ripgrep
=rg
- You can even use regex to search prime numbers. Not so easy for email addresses though.
Tips
- Use non-capturing groups, if needed
- If you are looking through file, use
find [path] -name [pattern]
or evenfind [path] -regex [pattern]
(typically with-type f
just for files) instead ofgrep