Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^(http|https)://[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)*(:\d+)?(/[^\s]*)?$

Matches a URL starting with http:// or https://, followed by a domain name, optional port, and optional path. The domain may include subdomains and hyphens.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'http://example.com'
( Start of a capturing group N/A
http Literal characters 'http' http
| Alternation (OR operator) N/A
https Literal characters 'https' https
) End of capturing group N/A
:// Literal characters '://' ://
[a-zA-Z0-9\-]+ Character class matching one or more alphanumeric characters or hyphens example, test-123
( Start of a non-capturing group (implicit in Python regex) N/A
\. Literal dot (escaped) .
[a-zA-Z0-9\-]+ Character class matching one or more alphanumeric characters or hyphens com, sub-domain
)* Quantifier: zero or more repetitions of the preceding group .com, .co.uk
( Start of a non-capturing group N/A
:\d+ Literal colon followed by one or more digits (port number) :8080
)? Quantifier: zero or one repetition of the preceding group :8080 or absent
( Start of a non-capturing group N/A
/[^\s]* Literal slash followed by zero or more non-whitespace characters /path/to/resource
)? Quantifier: zero or one repetition of the preceding group /path or absent
$ Anchor: asserts position at end of the string End of 'http://example.com/path'

Matches

  • http://example.com
  • https://sub.domain.co.uk:8080/path/to/resource
  • http://test-123.com
  • https://a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z
  • http://localhost:3000
  • https://123-abc.def.ghi

Does not match

  • ftp://example.com
  • http://example..com
  • http://example.com/path with spaces
  • http://-example.com
  • http://example.com:
  • http://example.com:abc
  • http://example.com/path?query=value

Common pitfalls

  • Forgetting to escape the dot in the domain part (should be \. instead of .).
  • Not anchoring the regex with ^ and $, leading to partial matches.
  • Assuming the regex handles all valid URLs (e.g., internationalized domain names or IPv6 addresses).
  • Overlooking that the path part does not allow spaces (uses [^\s]*).
  • Not accounting for uppercase/lowercase letters in the protocol (e.g., HTTP://example.com).

Performance notes

The pattern is generally efficient due to limited quantifiers and alternations. However, the nested quantifiers (e.g., * for subdomains) could lead to catastrophic backtracking if the input is maliciously crafted with many subdomains. Consider using possessive quantifiers or atomic groups 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

Does this regex validate all valid URLs?

No. It covers common cases but misses many valid URL formats, such as those with IPv6 addresses, internationalized domain names, or complex query strings.

Why is the domain part [a-zA-Z0-9\-]+ instead of a more permissive character class?

The character class restricts domains to alphanumeric characters and hyphens, which is a common convention for domain names. Some valid domains may include other characters, but this regex simplifies the pattern.

Can this regex match URLs with uppercase letters in the protocol (e.g., HTTP://example.com)?

No. The protocol part is case-sensitive and only matches 'http' or 'https' in lowercase.

Why is the path part (/[^\s]*) optional?

The path is optional in URLs. For example, 'http://example.com' is a valid URL without a path.