Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^(https?|ftp)://[^\s/$.?#]\.[^\s]*$

Matches URLs starting with http://, https://, or ftp://, followed by a domain-like structure without spaces, and ending with a valid TLD or path.

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
https? Matches 'http' followed by an optional 's' (http or https) http or https
| Alternation: OR operator between 'https?' and 'ftp' N/A
ftp Matches literal 'ftp' ftp
) End of the capturing group N/A
:// Matches literal '://' ://
[^\s/$.?#] Negated character class: matches any character except whitespace, '/', '$', '.', '?', or '#' a, b, 1, A, -
\. Escaped literal '.': matches a dot character .
[^\s]* Negated character class: matches zero or more characters that are not whitespace example, com/path, 123
$ Anchor: asserts position at end of the string End of 'http://example.com'

Matches

  • http://example.com
  • https://sub.domain.co.uk/path
  • ftp://files.example.org
  • http://a.b
  • https://1.2.3.4

Does not match

  • http://example..com
  • http://example.com/path with space
  • http://.example.com
  • http://example.com$path
  • http://example.com?query
  • example.com
  • http://
  • https:/example.com

Common pitfalls

  • Fails to validate proper domain structure (e.g., allows 'http://a..b').
  • Does not enforce TLD length or validity (e.g., 'http://a.b' is accepted).
  • Allows paths starting immediately after domain (e.g., 'http://example.compath').
  • Does not handle internationalized domain names (IDNs).
  • May match unintended strings if not anchored properly (e.g., 'prefixhttp://example.com').

Performance notes

No catastrophic backtracking risk. The pattern is linear in complexity. Anchors (^ and $) ensure efficient matching. Negated character classes ([^\s]) are efficient.

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 does the pattern allow 'http://a.b'?

The pattern only requires a single non-whitespace character before the dot and any non-whitespace after. It does not enforce domain structure rules like TLD length or valid characters.

Can this pattern match IPv4 addresses?

Yes, patterns like 'http://1.2.3.4' will match because the IP address is treated as a valid domain-like structure under the given rules.

Does this pattern support query parameters or fragments?

No, the pattern ends with [^\s]*$, which stops at the first whitespace. Query parameters (e.g., '?key=value') or fragments (e.g., '#section') will cause the match to fail unless they are part of the path.