A beginner-friendly introduction to regular expressions: common symbols, character classes, examples, and common mistakes to avoid.
Regex is a way to define patterns for finding or matching text. It looks intimidating at first, but a handful of building blocks cover most of what you’ll ever need.
A regular expression (regex, for short) is a sequence of characters that defines a search pattern. Instead of searching for one exact piece of text, a regex describes a shape that text can match, for example “any sequence of digits” or “a run of repeated spaces.” Regex shows up anywhere text needs to be searched, matched, validated, or replaced:
The simplest regex is just literal text. The pattern:
hello
matches the exact text “hello” wherever it appears. Most regex features build on top of this by adding special characters that stand in for “any character,” “zero or more,” and so on, rather than matching literally.
A small set of symbols covers most everyday regex use. Exact behavior can vary slightly between programming languages and regex engines, but the core ideas below are shared almost universally:
| Symbol | Meaning |
|---|---|
. |
Matches almost any single character (in most engines, any character except a newline by default) |
* |
Zero or more repetitions of whatever came before it |
+ |
One or more repetitions of whatever came before it |
? |
Makes the preceding item optional (zero or one), though it has a second, different meaning in some contexts |
^ |
Anchors a match to the start of the text (or line, depending on settings) |
$ |
Anchors a match to the end of the text (or line, depending on settings) |
Square brackets define a character class: a set of characters where any one of them counts as a match.
[abc]: matches exactly one character, as long as it’s a, b, or c[a-z]: matches any single lowercase letter, using a hyphen to define a range[0-9]: matches any single digitCombined with + or *, a character class can match a whole run of characters. For example, [0-9]+ matches one or more digits in a row.
| Pattern | Matches |
|---|---|
[0-9]+ |
One or more digits in a row, e.g. “42” or “2024” |
[A-Za-z]+ |
One or more letters in a row, e.g. “hello” |
\s{2,} |
Two or more whitespace characters in a row, useful for finding repeated spaces |
Real-world patterns can get much more elaborate: a “fully correct” email-matching regex, for instance, is notoriously long and still doesn’t perfectly cover every technically valid address. For everyday validation, a simpler pattern combined with an actual confirmation step, like sending a verification email, is usually more reliable than chasing a perfect regex. Regex is often combined with other data-handling tasks, such as validating a value before storing it in What Is JSON?, or matching a specific pattern within a URL: see What Is URL Encoding? if you’re working with URLs specifically.
Flags change how a pattern is applied, without changing the pattern itself. The exact names vary by language, but three are especially common:
^ and $ match the start and end of each line, rather than only the start and end of the whole text.In JavaScript, for example, these are written as g, i, and m after the pattern. Other languages expose the same ideas through differently named options or constants.
., *, and ? have special meaning in regex. To match them literally, they usually need a backslash in front, like \..* matches almost anything, which can accidentally match far more text than intended.^ and $ behave, what counts as a “word character,” or how flags are named, can differ between programming languages and regex engines. Always test a pattern in the actual environment it will run in.The best way to learn regex is to experiment with real patterns against real text. The Regex Tester lets you try a pattern against sample text and see exactly what it matches, without writing any code. Once you’re comfortable with patterns, tools like Find and Replace Text and Duplicate Line Remover put that pattern matching to practical use.
Regex Tester
Regex is short for “regular expression,” a sequence of characters that defines a search pattern for matching text.
The basics are approachable and cover most everyday needs: literal text, a handful of symbols, and character classes. Regex becomes more difficult as patterns grow more complex, but you don’t need to master everything at once.
Searching text, validating input like form fields, and finding-and-replacing text based on a pattern rather than an exact match.
. matches almost any character, and * means “zero or more” of whatever came before it. Together, .* matches any sequence of characters, including none at all, which makes it very broad and worth using carefully.
* matches zero or more repetitions, so it still matches even if the thing before it doesn’t appear at all. + requires at least one occurrence.
Mostly, but not entirely. The core syntax is shared across most languages, but specific features, flag names, and edge-case behavior can differ between regex engines. It’s worth testing a pattern in the language or tool you’re actually using.
Find the right tool, or keep reading Brekzy's other guides.