Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

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

Matches URLs with optional http/https and www prefixes, followed by a domain name and optional path/query parameters. The domain must end with a TLD of 1-6 letters.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
( Start of non-capturing group (optional) Optional group start
https? Literal 'http' followed by optional 's' http or https
:// Literal characters '://' ://
)? End of group, quantifier: 0 or 1 occurrence Optional http:// or https://
( Start of non-capturing group (optional) Optional group start
www\. Literal 'www.' www.
)? End of group, quantifier: 0 or 1 occurrence Optional www.
[-a-zA-Z0-9@:%._\+~#=] Character class: hyphen, letters, digits, @, :, %, ., _, +, ~, #, = a-z, A-Z, 0-9, @, :, %, ., _, +, ~, #, -
{1,256} Quantifier: between 1 and 256 occurrences of the preceding token 1 to 256 characters
\. Literal '.' (escaped) .
[a-zA-Z0-9()] Character class: letters, digits, parentheses a-z, A-Z, 0-9, (, )
{1,6} Quantifier: between 1 and 6 occurrences of the preceding token 1 to 6 characters
\b Word boundary: asserts position between word and non-word character End of TLD
( Start of capturing group for path/query Path/query start
[-a-zA-Z0-9()@:%_\+.~#?&//=] Character class: hyphen, letters, digits, @, :, %, _, +, ., ~, #, ?, &, /, = a-z, A-Z, 0-9, @, :, %, _, +, ., ~, #, ?, &, /, -
* Quantifier: 0 or more occurrences of the preceding token Zero or more path/query characters
) End of capturing group End of path/query
$ Anchor: asserts position at end of the string End of string

Matches

  • https://www.example.com
  • http://example.com/path?query=1
  • www.example.co.uk
  • example.com
  • https://sub.domain.com:8080/path/to/resource?param=value#fragment

Does not match

  • ftp://example.com
  • example..com
  • example.c
  • example.123
  • http://example.com/ invalid
  • https://example.com/path with spaces

Common pitfalls

  • Overly permissive character classes may match unintended characters (e.g., spaces in paths).
  • The {1,256} quantifier may cause performance issues with very long strings due to backtracking.
  • The \b word boundary may not work as expected with non-ASCII characters or trailing punctuation.
  • The pattern does not validate TLDs against a list of known TLDs (e.g., .com, .org).

Performance notes

The pattern is generally efficient but may suffer from catastrophic backtracking if the input string is very long and contains many repeated characters in the domain or path segments. Anchoring (^ and $) ensures the entire string is validated. Consider using atomic groups or possessive quantifiers if performance is critical.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why is the character class for the domain so permissive?

The character class [-a-zA-Z0-9@:%._\+~#=] is designed to match most valid domain characters, including special characters like @, :, %, and +. This is common in URL patterns but may match unintended characters in some contexts.

Does this pattern validate all possible URLs?

No. It does not validate TLDs against a list of known TLDs (e.g., .com, .org) and may match invalid or non-existent domains. It also does not validate port numbers or IP addresses.

Can this pattern match URLs with internationalized domain names (IDNs)?

No. The pattern does not account for IDNs (e.g., '例.测试') or punycode-encoded domains. Additional processing is required for full IDN support.

Why is the {1,256} quantifier used for the domain part?

The {1,256} quantifier limits the domain part to 256 characters, which is a reasonable upper bound for domain names according to RFC standards. However, this may not cover all edge cases.