MANUAL
Regex for beginners
Regular expressions for beginners
A regular expression (regex) is a search pattern. You use it to find, check or replace text: is this a valid postcode? Find all the telephone numbers in this file.
01
The building blocks
| Symbol | Meaning | Example | Found |
|---|---|---|---|
abc | exactly this text | kat | "cat" |
. | any character (except a new line) | k.t | "cat", "kit" |
\d | a digit | \d\d | "42" |
\w | letter, number or _ | \w+ | "invoice_1" |
\s | space, tab or new line | a\sb | "a b" |
[abc] | one of these characters | [aeiou] | a vowel |
[^abc] | anything except these | [^0-9] | no digits |
* | 0 or more times | ab* | "a", "abbb" |
+ | 1 or more times | \d+ | "7", "2026" |
? | 0 or 1 time | kleuren? | "colour", "colours" |
{n,m} | n to m times | \d{4} | exactly 4 digits |
^ and $ | start and end | ^\d{4}$ | only "2026" |
( ) | group | (ab)+ | "abab" |
| | or | jpg|png | "jpg" or "png" |
02
Flags: how the regex searches
Flags are placed after the pattern (/patroon/flags). The most important ones:
| Flag | Name | What it does |
|---|---|---|
i | case-insensitive | upper and lower case letters are treated as the same |
g | global (JavaScript) | all matches rather than just the first one |
m | multiline | ^ and $ work on a per-line basis |
s | dotall | . also matches a new line |
u | Unicode | correct handling of é, ü and emoji |
x | extended (PHP, Python) | spaces and comments allowed in your pattern |
Please note: not every language supports the same flags. The regex-flags-cross-language skill provides an overview for each language.
03
Example: Dutch postcode
^[1-9][0-9]{3}\s?[A-Z]{2}$
^start[1-9]the first digit is not 0[0-9]{3}three more digits\s?optionally a space[A-Z]{2}two uppercase letters$end
If the i flag is set, lower-case letters are also permitted.
04
Always test using examples
Before you write the regex, make a note of what should and what shouldn’t match:
| Must fit | Must not fit |
|---|---|
| 9711 AB | 0711 AB |
| 9711AB | 971 AB |
1234 zz (with i) | 9711 ABC |
Test it in the language you’re using it in. An online tester often uses a different ‘flavour’ (JavaScript or PCRE) to your code.
05
Regex in VS Code
Press Ctrl+F (Mac: Cmd+F) or Ctrl+Shift+F to search all files, and click the .* icon to enable regular expressions. When replacing, you can use groups: search for (\w+)@oud\.nl, replace with $1@nieuw.nl.
06
Regex in Laravel validation
$request->validate([
'postcode' => ['required', 'regex:/^[1-9][0-9]{3}\s?[A-Z]{2}$/i'],
]);
Use array notation if your pattern contains a |. In string notation ('required|regex:...'), Laravel splits the string at | and your regex will break.
07
When there is no regex
- Email addresses and URLs: use the built-in
emailandurlrules. - Reading HTML or JSON: use a parser (see Reading and validating JSON).
- Complex patterns with nested
+and*characters: which can become extremely slow when processing long text.
08
Need help?
Validation and input checks that really work are an integral part of Bespoke software and web applications.
09
Skills
- [Regex builder] (/nl/skills/regex-builder)
- [Regex flags across languages] (/nl/skills/regex-flags-cross-language)
10
Related services
- [Maatwerk software en webapplicaties] (/nl/services#service-03)