Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Code GenerationbeginnerNew

Regex Patterns

Share

Common regex patterns for email, URL, phone, IP validation

Works with OpenClaude

You are a regex pattern expert. The user wants to validate emails, URLs, phone numbers, and IP addresses using battle-tested regex patterns.

What to check first

  • Confirm your regex engine supports standard PCRE or JavaScript RegExp syntax
  • Decide if you need to validate format only or also check deliverability (regex can't verify live emails)
  • Check if your target phone numbers have a specific country code requirement (US, international, etc.)

Steps

  1. Choose the validation type: email, URL, phone number, or IPv4/IPv6 address
  2. Select the regex pattern appropriate to your use case (strict validation vs. practical tolerance)
  3. Create a RegExp object with the new RegExp() constructor or literal /pattern/ syntax
  4. Call .test() to check if a string matches, or .exec() to extract matches
  5. For email, know that RFC 5322 is overly complex—use a practical pattern instead
  6. For URLs, decide if you need protocol (http://) or allow protocol-relative (//)
  7. For phone numbers, handle country codes and formatting (spaces, dashes, parentheses)
  8. Test edge cases: empty strings, whitespace, special characters, boundary conditions

Code

// Email validation - practical RFC 5322 subset
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// Stricter: /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

// URL validation - with optional protocol
const urlPattern = /^(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)$/;

// US Phone Number - flexible formatting
const usPhonePattern = /^(\+?1[-.\s]?)?\(?[2-9]\d{2}\)?[-.\s]?[2-9]\d{2}[-.\s]?\d{4}$/;
// International format: /^\+?[1-9]\d{1,14}$/

// IPv4 Address
const ipv4Pattern = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;

// IPv6 Address - simplified (full RFC 4291 is extremely complex)
const ipv6Pattern = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
regexpatternsvalidation

Install command:

curl -o ~/.claude/skills/regex-patterns.md https://clskills.in/skills/code-generation/regex-patterns.md

Related Code Generation Skills

Other Claude Code skills in the same category — free to download.

Want a Code Generation skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.