Regular expressions for beginners: patterns you will actually use

Learn the core regex syntax step by step, with practical examples and the mistakes to avoid.

A regular expression, or regex, is a compact way of describing a pattern in text. It might look like line noise at first, but a small part of the syntax handles the great majority of practical jobs: finding dates in a log, tidying a list, checking the shape of an input or replacing a format across a whole file.

Literal text and special characters

The simplest pattern is plain text. The regex cat finds those three letters in order, including inside "concatenate". A few characters have a special meaning and need a backslash in front of them when you want them literally: . * + ? ^ $ ( ) [ ] { } | and the backslash itself. So to find "3.5", write 3\.5, because a bare dot means "any character".

Character classes

Square brackets match one character from a set:

  • [abc] matches a, b or c.
  • [0-9] matches a digit, and [a-z] a lowercase English letter.
  • [^0-9] matches any character that is not a digit.

Handy shortcuts exist for common sets. \d stands for a digit, \w for a word character and \s for whitespace. Their capital forms are the opposites, so \D means "not a digit".

Be careful with non-English letters. In many engines [a-z] and sometimes \w do not include ş, ğ, ç or ı. For Turkish text, add those letters explicitly or use Unicode classes such as \p{L}, where the engine supports them, with the appropriate flag.

Quantifiers

Quantifiers say how many times the preceding item may repeat:

  • ? means zero or one.
  • * means zero or more.
  • + means one or more.
  • {3} means exactly three, and {2,4} means between two and four.

So \d{4} finds a four-digit number, and colou?r matches both "color" and "colour".

Quantifiers are greedy by default, which means they take as much text as possible. On the text <b>one</b> <b>two</b>, the pattern <b>.*</b> matches the whole line rather than the first tag pair. Adding a question mark makes it lazy: <b>.*?</b> stops at the first possible end.

Anchors and word boundaries

Anchors match positions instead of characters.

  • ^ marks the start of the text, or of each line in multiline mode.
  • $ marks the end.
  • \b marks a boundary between a word and a non-word character.

The pattern \bcat\b finds "cat" as a whole word and ignores "concatenate". Validating that a whole value has a given shape needs both anchors: ^\d{5}$ accepts exactly five digits, while \d{5} alone would also accept a line with ten digits.

Groups and alternatives

Parentheses group part of a pattern and also remember what it matched. The vertical bar means "or".

  • (jpg|png|webp) matches any of the three words.
  • (\d{2})-(\d{2})-(\d{4}) captures day, month and year separately.

Captured groups are what make replacing so powerful. Given the pattern above, a replacement of $3-$2-$1 turns 14-03-2026 into 2026-03-14 on every matching line. Some editors write this as \3-\2-\1. If you only need to group without capturing, use (?:...).

Flags

Flags change how the whole pattern behaves. The common ones are:

  • i: ignore case.
  • g: find all matches instead of only the first.
  • m: make ^ and $ work on each line.
  • s: let the dot match line breaks too.
  • u: enable full Unicode handling, in the engines that use this flag.

Case-insensitive matching with Turkish needs care, because the relationship between I, ı, İ and i differs from English. Test with real data rather than assuming the engine handles it.

Patterns you can reuse

  • A date in ISO form: \b\d{4}-\d{2}-\d{2}\b
  • Blank lines: ^\s*$ in multiline mode.
  • Trailing spaces at the end of each line: [ \t]+$ in multiline mode.
  • Two identical adjacent words: \b(\w+)\s+\1\b
  • A simple shape check for an e-mail address: ^[^\s@]+@[^\s@]+\.[^\s@]+$

That last pattern is only a rough sanity check. The full rules for e-mail addresses are very complicated, and the only reliable test is sending a confirmation message.

When not to use regex

Regular expressions are excellent for flat text but poorly suited to nested structures. HTML, JSON and programming languages should be handled by a proper parser. A regex that seems to work on a sample of HTML will often fail on real pages.

Performance is another concern. Nested quantifiers like (a+)+ can take an exponentially long time on certain inputs, a problem called catastrophic backtracking. If a server applies a regex to user input, keep the patterns simple and set a timeout.

Working safely

Build a pattern gradually, adding one part at a time and testing it on realistic samples that include things which should not match. Before a bulk replacement, keep a copy, count the matches, and compare the versions before and after. A mistaken replace-all can do a lot of damage in a fraction of a second, whereas a quick comparison reveals the problem immediately.

Dialects also differ. JavaScript, Python, .NET, PCRE and text editors each have small variations, particularly around lookbehind, named groups and Unicode. A pattern that works in one place may need minor changes in another.

Tools mentioned in this guide