A Gentle Introduction to Regular Expressions

Basic Matching

/efg/
/[TN]ed/
/\d/
  • . – matches any character, except newline
  • \w – matches any word character (non-digit and non-whitespace)
  • \W – matches any non-word character
  • \s – matches any whitespace character such as space or newline
  • \S – matches any non-whitespace character
  • \D – matches any non-digit character

Capture Groups

/(\w)b\1/
/(?<myGroup>goose)\k<myGroup>/

Repetition

  • (abc)* – matches any number of repetitions of “abc”, even zero
  • (abc)+ – matches at least one repetition of “abc”
  • (abc)? – matches any zero or one repetitions of “abc”, but no more
  • (abc){n} – matches exactly n repetitions of “abc”
  • (abc){n,} – matches at least n repetitions of “abc”
  • (abc){n,m} – matches anywhere from n to m repetitions of “abc”

Boundaries

/(goose)$/
/\b(she)\b/

Advanced Boundaries

/(?<!b)(123)/

Escape Characters

Further Resources

Full stack software developer by day. Writer, illustrator, and general creative dabbler by night.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Android: Simple and Easy approach to Test Driven Development

Create more than 16 Scoring Meshes in Geant4

Docker-compose and build environment that sings!

New Bike Day Dev Journal: NO, YOU’RE A BAD GATEWAY

Taming The Elephant

New ARK Core v2 Released

Occupancy App — An open source, globally available space counter

Should we use TDD or BDD… or both?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Edward Saavedra

Edward Saavedra

Full stack software developer by day. Writer, illustrator, and general creative dabbler by night.

More from Medium

Get started with Git: An intro

CS50P Problem Set 2 Coke Machine

Instance method, Class method, and Static method

Starting with Git and Github