Regular Expressions That Work: A Tested Cookbook for Everyday Data
Tested regex patterns for dates, emails, postcodes, IP addresses and more, how flags and anchors change results, and how flavours differ between languages.

Regular Expressions That Work: A Tested Cookbook for Everyday Data
Copying a regular expression from a forum answer is quick, and it is also how many subtle bugs start. The pattern looks right, passes the two examples you try, and then accepts month 13, rejects a perfectly valid email address, or freezes the server on an unusual input. The free Regex Cookbook and Tester gives you 62 patterns that are each tested against examples they must match and counter-examples they must reject, a live tester for your own patterns, and ready code for six languages.
Anchors decide the job
The same pattern does two different jobs depending on anchors. With ^ at the start and $ at the end, it validates a whole value, such as a form field that must be a date. Without them, it searches inside longer text and finds every date in a document. Most cookbook entries are validators. To search, remove the anchors and add the g flag.
The flags
gfinds every match instead of stopping at the first.iignores case.mmakes^and$match at each line break, useful for log files.slets the dot match line breaks.uturns on Unicode mode, needed for\p{L}(letters in any script) and emoji properties.
What the cookbook covers
The patterns are grouped by job. Numbers include integers, decimals, thousands separators, scientific notation and percentages. Dates and times cover ISO dates, UK and US formats, 24-hour and 12-hour times and full ISO 8601 timestamps with time zones. Web patterns handle URLs, domains, IPv4 and IPv6, MAC addresses, hex colours and URL slugs. Identity patterns cover UK and Canadian postcodes, US ZIP codes, UK National Insurance numbers, IBAN shapes, card number prefixes, ISBNs, UUIDs, semantic versions and E.164 phone numbers. There are also text-cleaning patterns, code patterns and security scanners for leaked AWS keys and private key headers.
Each entry shows what it matches and what it rejects, so you can judge whether it suits your data. For example, the UK postcode pattern rejects a first letter of Q, because no real postcode area starts with it.
Shape is not meaning
A regular expression checks the form of text, not whether it is true. A date pattern cannot know that 31 February does not exist. An IBAN pattern cannot verify the check digits. An email pattern cannot prove the mailbox exists. Use patterns to reject obvious mistakes early, then validate properly in code or by sending a confirmation.
Flavours differ
JavaScript, Python, Java, .NET and PHP share most syntax, but the details trip people up. Python wants raw strings so backslashes survive. Java needs every backslash doubled inside a string literal. C# verbatim strings double quote marks instead. Go and RE2 leave out lookarounds and backreferences on purpose, so matching always takes linear time. The tool writes the snippet for each language with the flags converted, and warns when a pattern will not compile in Go.
Avoiding catastrophic backtracking
Patterns with nested quantifiers, such as (a+)+ or (.), can take exponential time on certain inputs. An attacker can exploit this to freeze a server, a problem known as ReDoS. Keep patterns that run on user input simple, prefer specific character classes over .*, and use time limits or a linear-time engine where available. The tester warns when it sees a nested quantifier.
Escaping literal text
When you build a pattern from text, such as a product code containing dots and brackets, every special character must be escaped. The escaper does this for you, so price (USD) = $9.99 becomes a pattern that matches exactly that text.
Step by step
- Choose to test, browse the cookbook or escape text.
- Pick a cookbook pattern or write your own.
- Set the flags and paste test text.
- Check the highlighted matches and capture groups.
- Copy the snippet for your language.
Privacy
The tester runs in your browser, so log files and customer data used as test text stay on your device.



