A Beginner's Guide to Writing Regex

Regex is a short but dense language for finding a specific pattern inside text. It looks intimidating at first, but learning a handful of core symbols covers most everyday needs.

Summary

Regex is a short but dense language for finding a specific pattern inside text. It looks intimidating at first, but learning a handful of core symbols covers most everyday needs.

Step by step

  1. Start with the most common symbols: a dot matches any character, an asterisk means repetition, square brackets define a set of characters.
  2. Practice on a simple goal, like finding every email address in a block of text.
  3. Test what you write in a live tool so you can actually see which part of the text matches.
  4. Before writing something complicated, ask whether you really need regex or whether a simple text function would do. Regex isn't always the simplest fix.

Why it matters

Regex lets you do in one line what would take hours by hand across hundreds of lines of text: pulling every error code out of a log file, normalizing phone numbers in a spreadsheet, or checking whether a form field was filled in correctly. A badly written pattern produces wrong results just as fast, though. It can quietly delete too much or skip a line that mattered. What counts is how well you test, not how much you write.

A few more concepts worth knowing

  • Anchors: ^ marks the start of a line and $ the end. When validating a whole field, leaving them out means the pattern can match anywhere inside the text.
  • Escaping: the dot is special, so to match a literal dot you put a backslash before it. The same goes for parentheses, plus signs and question marks.
  • Greedy vs lazy: the asterisk and plus grab as many characters as they can by default. Add a question mark after them and they take the shortest possible match instead.
  • Flags: settings like case-insensitive or multiline mode change how a pattern behaves.
  • Shorthands: \d is a digit, \s is whitespace, \w is a letter, digit or underscore.

Common mistakes

  • Copying a pattern from one language to another unchanged. JavaScript, Python and database engines support different features.
  • Assuming \w covers accented or non-English letters everywhere. In some engines, characters like ş, ğ or é will not match.
  • Writing nested repetition. On certain inputs the engine can hang for a very long time.
  • Trying to catch every valid email address with one perfect pattern. In practice, a simple check plus a confirmation email works better.

An example

A customer list has phone numbers written every which way: some with spaces, some with dashes, some starting with a zero. You want them all in one format. First you strip out non-digit characters like spaces, dashes and parentheses with one pattern. Then you write a second pattern that uses the ^ anchor to check for a leading zero. Before running either step on the full list, you try it on ten or so different sample lines in a test tool and fix the pattern whenever an unexpected line turns up.

When not to use regex

Parsing nested structures like HTML, JSON or XML with regex tends to break easily. Use the language's own parser instead. For dates, currencies or addresses, an existing validation library is usually more reliable too.