- Regular Expressions = "regex" or "regexp"
- Comes from the
ededitor but you'll mostly encounter the
- A quick way of describing a particular pattern of characters in text
- Allows for extremely effective search and replace
- Comes from the
ededitor from UNIX but you'll mostly encounter the
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
BRE vs ERE
grepuses 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:
- EREs can be turned on in
grepby passing the
- More details here
- The origins of grep: Brian Kernighan, one of the forefathers of UNIX discusses how
grepcame 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".
- There are also some alternatives to
- You can even use regex to search prime numbers. Not so easy for email addresses though.
- Use non-capturing groups, if needed
- If you are looking through file, use
find [path] -name [pattern]or even
find [path] -regex [pattern](typically with
-type fjust for files) instead of