PatternRegex
Match a web URL
Find http and https links in free text — commit messages, chat logs, scraped pages — without dragging in half the punctuation around them.
Last updated
Matches
- https://docs.example.com/v2/setup
- http://example.com
- https://a-b.co/x?y=1
Doesn’t match
- ftp://files.example.com
- example.com
- mailto:me@example.com
Why it’s written this way
https? makes the s optional so one pattern covers both schemes. The host is [\w.-]+ — letters, digits, dots and hyphens — and the whole path group is optional, so a bare domain link still matches.
The path's character class is deliberately narrow. URL-matching patterns mostly fail by being greedy: allow every legal URL character and the match swallows the closing bracket, comma or full stop that follows the link in prose.
Edge cases to know
- →Query strings using characters outside [\w./?%&=-] (like + or #fragments) are cut short at that character.
- →It does not validate the domain has a real TLD — ftp://, mailto: and bare hosts are out of scope by design.
- →For parsing a URL you already have (rather than finding them in text), use a URL parser, not a regex.