Learn regular expressions

  • data-coding
  • learn
  • regexp
  • spring

Intro

  • Regular Expressions = "regex" or "regexp"
  • Comes from the ed editor but you'll mostly encounter the grep 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 the grep 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?+{}
  • EREs can be turned on in grep by passing the -E parameter.
  • More details here

Tips

  • 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 f just for files) instead of grep
Metadata