Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
Python

r"
^(https?:\/\/)?(www\.)?([a-zA-Z0-9](?!.*--)[a-zA-Z0-9-]{1,61}[a-zA-Z0-9])(\.(?!.*--)(?!.*---)[a-zA-Z0-9](?!.*--)[a-zA-Z0-9-]{1,61}[a-zA-Z0-9])*\.[a-zA-Z]{2,}/?$
"
gm

Description

This regex pattern is designed to validate URLs with the following key features:

  1. Protocol Support: Optionally matches http:// or https://.
  2. Subdomain Handling: Supports optional www. and valid subdomains.
  3. Domain Name Validation: Ensures proper formatting of domain names:
    • Prevents double hyphens (--) in domain or subdomain names.
    • Enforces domain name length between 1 and 63 characters per label.
  4. Top-Level Domain (TLD): Validates TLDs with 2 or more alphabetic characters.
  5. Trailing Slash: Optionally allows a trailing slash (/) at the end of the URL.

Example Matches:

  • https://www.example.com
  • http://example.org
  • example.net/

Example Non-Matches:

  • http://-example.com (invalid hyphen usage)
  • https://example..com (double dot)
  • ftp://example.com (unsupported protocol)

Feel free to test and tweak as per your requirements! 🎯

Submitted by Hadi Mousavi - 5 days ago