Skip to main content
Dev · 283 tools

Common Regex Pattern Generator

Provides common, ready-to-use regular expressions with plain-English explanations

Regex PatternExample — click Generate for yours
A copy-paste starter for url input: /^https?:\/\/(?:[\w-]+\.)+[a-z]{2,}(?:[\/?#][^\s]*)?$/i Use case: validating url values. http or https URL with an optional path, query string, or fragment. For example, it matches http://example.org/docs?page=2 but rejects example.com/about. In an HTML pattern attribute, leave out the slashes and anchors, because the browser anchors the whole value for you. Show a friendly error message when the pattern fails, not the pattern itself. Caveat: For anything security-sensitive, parse with your platform's URL parser instead. Note: If your language offers a built-in parser for this format, prefer it over regex.

Generated 91 words · 653 characters

See 3 examples
  • Example 1
    A copy-paste starter for url input:
    
    /^https?:\/\/(?:[\w-]+\.)+[a-z]{2,}(?:[\/?#][^\s]*)?$/i
    
    Use case: validating url values. http or https URL with an optional path, query string, or fragment. For example, it matches http://example.org/docs?page=2 but rejects example.com/about.
    
    In an HTML pattern attribute, leave out the slashes and anchors, because the browser anchors the whole value for you. Show a friendly error message when the pattern fails, not the pattern itself.
    
    Caveat: For anything security-sensitive, parse with your platform's URL parser instead. Note: If your language offers a built-in parser for this format, prefer it over regex.
  • Example 2
    Solid starting point for url form fields:
    
    /^https?:\/\/(?:[\w-]+\.)+[a-z]{2,}(?:[\/?#][^\s]*)?$/i
    
    Use case: validating url values. http or https URL with an optional path, query string, or fragment. For example, it matches https://example.com but rejects ftp://files.example.com/readme.txt.
    
    In a code review, keep a short comment above the pattern explaining what it accepts. Normalize the input to lowercase first where case does not matter.
    
    Caveat: For anything security-sensitive, parse with your platform's URL parser instead. Note: Validation should guide people to a fix, not just reject them.
  • Example 3
    Reach for this when validating slug input:
    
    /^[a-z][a-z0-9-]{1,62}[a-z0-9]$/
    
    Use case: validating slug values. Slug that must start with a letter and end with a letter or digit. For example, it matches hello-world but rejects café-menu.
    
    In Go, the regexp package does not support lookarounds, so pick a variant without them. Log rejected inputs, with care for privacy, so you can improve the pattern over time.
    
    Caveat: Nested, overlapping quantifiers can cause catastrophic backtracking; keep them out of your edits. Tip: Online regex testers let you paste the pattern and watch each part light up.

About this generator

Common validation patterns — emails, URLs, phone numbers, dates, hex colors, slugs — are written repeatedly across codebases, often from memory, and small errors in anchoring or character classes cause validation bugs that are hard to catch. This generator returns a ready-to-use regex pattern for one of six common validation targets along with a plain-English explanation of exactly what the pattern matches and what it does not. The pattern for dropdown selects the target, and each target has eight or nine well-known variants: email ranges from a deliberately loose no-spaces-and-one-@ check to versions with length limits, plus-tags, or a single allowed domain; url covers http/https/ftp, optional ports, localhost and IPv4 hosts, https-only, and scheme-optional website fields; phone covers E.164, flexible international formats, North American numbers with or without +1 and extensions, and UK mobiles; date covers ISO 8601 dates and date-times, MM/DD/YYYY, DD/MM/YYYY, dotted and slashed variants, and year-month; hex color covers 3-, 4-, 6-, and 8-digit codes, optional hashes, and captured RGB channels; slug covers hyphen or underscore separators, length limits, nested paths, and date-prefixed blog slugs. Random draws from all six. Each result shows the pattern, what it matches, a sample value it accepts and one it rejects (checked against the pattern itself), plus usage tips and a caveat. Before using a pattern in production, test it against real inputs and known edge cases for your specific use case. Each pattern is a practical starting point — email patterns in particular are intentionally simple rather than exhaustively spec-compliant, which is the right trade-off for most applications.

How to use

  1. Choose what you want to match, or leave it on random.
  2. Click Generate to get a pattern and its explanation.
  3. Copy the pattern into your validation code.
  4. Test it against real and edge-case inputs before relying on it.

Use Cases

  • •Validating emails, URLs, phone numbers, and dates
  • •Starting from a tested pattern instead of writing from scratch
  • •Learning how common regex patterns are built
  • •Documentation and code-comment examples
  • •Quick reference when you forget a pattern

Tips

  • →Always test a regex against edge cases, not just the happy path.
  • →Mind escaping — some languages require backslashes to be doubled in strings.
  • →For emails, a simple format check plus a confirmation email beats a perfect regex.
  • →Anchor patterns with ^ and $ when you mean to match the whole string.

FAQ

what is a regular expression

A regular expression, or regex, is a pattern that describes a set of strings, used to search, match, and validate text. For example, an email regex checks whether a string has the shape of an email address. Regex is supported in virtually every programming language.

is there a perfect email regex

No — fully validating every legal email address by regex is famously impractical, since the official specification is extraordinarily permissive. A simple pattern that checks for characters, an @, and a domain catches the vast majority of mistakes; for true verification, send a confirmation email.

how do I use these patterns in my language

Most languages have a regex engine with similar syntax. Paste the pattern into your language's regex constructor or literal, mind any escaping rules (some languages need backslashes doubled in strings), and test it against real examples before relying on it.

does the generator anchor patterns to match the whole string

Yes. Every pattern starts with ^ and ends with $, so it must match the whole value rather than any substring of it. Always verify anchoring behavior for your specific use case — some engines and attributes (such as the HTML pattern attribute) anchor automatically, while others treat patterns as substring searches by default.

You might also like

Popular tools from other categories that share themes with this one.