Common regex patterns: 15 tested examples with explanations

Most regex you'll write is a variation on the same dozen patterns. Here are fifteen common ones, each tested in Python and JavaScript, with a plain explanation of how they work and where they fall short. Copy them, but read the caveats: a regex checks format, not truth.

Updated 9 October 2026 · tmcmoney

How to read these

Patterns marked "full match" use ^ and $ anchors, so the whole string must match. Use them for validating a single field. Remove the anchors to search inside longer text. All patterns were tested with Python's re module and JavaScript's RegExp, which share this syntax.

Dates and times

1. ISO date (YYYY-MM-DD), full match

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Matches 2026-10-09 and 2027-12-31. Rejects 2026-13-01 and 2026-1-9. Month must be 01 to 12 and day 01 to 31. It doesn't know that February has 28 or 29 days, so check real validity with a date library.

2. UK date (DD/MM/YYYY), full match

^(0[1-9]|[12]\d|3[01])/(0[1-9]|1[0-2])/\d{4}$

Matches 09/10/2026. Rejects 9/10/2026 (no leading zero), 32/01/2026 and 10/13/2026. Same caveat about month lengths.

3. 24-hour time (HH:MM), full match

^([01]\d|2[0-3]):[0-5]\d$

Matches 09:30, 23:59 and 00:00. Rejects 24:00, 9:30 and 12:60.

UK formats

4. UK postcode (format check), full match

^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$

Matches BT1 5GS, SW1A 1AA, M1 1AE, BT71 4AB and EC1A1BB. Rejects BT1 and 12345. Uppercase the input and trim it first. This checks the shape only: it will accept postcodes that don't exist. To check a real address, use a postcode lookup service.

5. UK mobile number, full match

^(?:\+44\s?7|07)\d{3}\s?\d{6}$

Matches 07700 900123, +44 7700 900123 and 07700900123. Rejects landlines such as 01632 960123. For anything beyond simple forms, strip spaces and use a phone number library.

6. UK VAT number (GB plus 9 digits), full match

^GB\d{9}$

Matches GB123456789. The standard format is 9 digits, but some VAT numbers use other formats (branch traders add 3 digits, for example), so treat this as a starting point.

Web and data

7. Simple email check, full match

^[^\s@]+@[^\s@]+\.[^\s@]+$

Something, an at sign, something, a dot, something, with no spaces. That's deliberately loose. Fully validating email addresses with regex is a rabbit hole. The only real test is sending a confirmation email.

8. Find URLs in text

https?://[^\s<>"]+

Finds http and https links up to the next space, quote or angle bracket. Pitfall: it includes trailing punctuation, so "see https://a.b/c." captures the full stop. Strip trailing .,;:) afterwards.

9. URL slug, full match

^[a-z0-9]+(?:-[a-z0-9]+)*$

Matches common-regex-patterns and a1. Rejects capitals, double hyphens and leading or trailing hyphens.

10. Hex colour, full match

^#(?:[0-9a-fA-F]{3}){1,2}$

Matches #fff and #1c2a44. Rejects #ffff and colours without the hash.

11. IPv4 address, full match

^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$

Matches 192.168.0.1 and 255.255.255.255. Rejects 256.1.1.1 and 1.2.3. Each part is 0 to 255 with no leading zeros. In Python, the ipaddress module is simpler and stricter.

Money and numbers

12. Pounds amount, full match

^£?\d{1,3}(,\d{3})*(\.\d{2})?$

Matches £1,250.00 and 99. Rejects £12.5 (pence need two digits). Remove the commas before converting to a number, and never store money as a float.

13. Invoice number like INV-2026-001, full match

^INV-\d{4}-\d{3,}$

Matches INV-2026-001 and INV-2026-1042. Rejects INV-26-001 and lowercase inv. Useful for checking a sequence in a spreadsheet export.

Cleaning text

14. Repeated words

\b(\w+)\s+\1\b (case-insensitive)

Finds "the the" and "sat sat". The \1 is a backreference to whatever the first group matched. Great for proofreading.

15. Trailing whitespace and repeated spaces

Trailing: [ \t]+$ with the multiline flag, replace with nothing. Repeated spaces: \s{2,}, replace with a single space. Careful: \s includes newlines, so use {2,} (a space) if you want to keep line breaks.

Pitfalls to remember

  • Anchor validation patterns. Without ^ and $, "abc2026-10-09xyz" passes a date check.
  • Greedy by default. .* matches as much as possible. Use .*? for the shortest match.
  • Escape special characters. A literal dot is \., and a literal plus is \+.
  • Use raw strings in Python. Write r"\d+" so backslashes aren't swallowed.
  • Don't parse HTML with regex. Use a proper parser for anything more than quick one-off searches.
  • Beware catastrophic backtracking. Nested quantifiers like (a+)+ can hang on long input. Avoid them in anything that processes user input.
  • Test with bad input, not just good. Keep a list of strings that should fail and check them every time you change a pattern.

100 tested patterns in one reference

The Regex Reference is a PDF with 100 tested patterns and explanations, a testing worksheet and a pitfalls checklist. Pay once on Gumroad and download straight away.

Frequently asked questions

What is a regex for a UK postcode?

A common format check is ^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$ applied to uppercased input. It checks the shape only and will accept postcodes that don't exist.

What is the best regex for email validation?

A loose check such as ^[^\s@]+@[^\s@]+\.[^\s@]+$ catches obvious typos. Full validation by regex is impractical. Sending a confirmation email is the only reliable test.

Do Python and JavaScript use the same regex syntax?

For common patterns like these, yes. There are differences in advanced features and flags, so test patterns in the language you will use.

More guides

General information, not legal, tax or financial advice. Check the official sources linked above before you rely on them.