Regular Expressions 101

Community Patterns

URL validation regexp

0

Regular Expression
PCRE2 (PHP >=7.3)

/
^(?:(http?|s?ftp):\/\/|file:\/\/\/)?(([\P{Cc}]+):([\P{Cc}]+)@)?([a-zA-Z0-9][a-zA-Z0-9.-]*)(:[0-9]{1,5})?($|\/[\w\-\._~:\/?[\]@!\$&'\(\)\*\+,;=.]+(\#[\w]*)?$)
/
gixm

Description

Regexp to check URL validity and extract subparts.

  • Allows HTTP / HTTPs / sFTP / FTP / FTPs protocols
  • Allows HTTP / FTP authentication included in URL
  • Allows internal domain names
  • Allow non default port protols

You can extract subpart components of your URL :

  • protocol
  • domain name
  • authentication
  • port number
  • subdir
  • anchor
Submitted by Damien Cuvillier - 2 years ago