Regular Expressions 101

Community Patterns

URL parser

0

Regular Expression
PCRE (PHP <7.3)

/
^(?#protocol)(?<protocol>(?:ht|f)tps?)\:(?:\/\/)?(?#user/password)(?:(?<user>\S+?)(?::(?<password>\S+))?@)?(?#domaine)(?<domain>(?:(?:[a-z\x{00a1}-\x{ffff}0-9-]++\.)+(?#top_level_domain)(?<top_level_domain>[a-z\x{00a1}-\x{ffff}]{2,}))|(?<ip>(?:1?\d{1,2}|2[0-4]\d|25[0-5])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){3}))(?#port)(?::(?<port>\d{1,5}))?(?#directory)(?<directory>\/(?:[^?#\s])*)?(?#query)(?:\?(?<query>[^#\s]*))?(?#anchor)(?:#(?<anchor>[^\s]*))?$
/
guim

Description

Parse a URL and return its components

From http://userid:password@example.com/file1/file2?test=1&test=2#here you will get:

  • Full match = http://userid:password@example.com/file1/file2?test=1&test=2#here
  • Group protocol = http
  • Group user = userid
  • Group password = password
  • Group domain = example.com
  • Group top_level_domain = com
  • Group directory = /file1/file2
  • Group query = test=1&test=2
  • Group anchor = here
Submitted by Gilles Marchand - 7 years ago