Skip to content

Instantly share code, notes, and snippets.

@boekkooi
Created November 26, 2014 09:03
Show Gist options
  • Save boekkooi/abdd30d472b7b6bd3227 to your computer and use it in GitHub Desktop.
Save boekkooi/abdd30d472b7b6bd3227 to your computer and use it in GitHub Desktop.
RFC3261 Uri Regex
<?php
const SIP_PATTERN = '~^
(?:(?<schema>sip(s)?):)?
(?:
(?<user>(?:[a-z0-9\-_\.!\~*\'\(\)]|%[0-9a-f]{2}|[&=\+$,;?\/])+) # user ( unreserved / escaped / user-unreserved )
(?::(?<password>(?:[a-z0-9\-_\.!\~*\'\(\)]|%[0-9a-f]{2}|[&=\+$,])+))? # password ( unreserved / escaped / "&" / "=" / "+" / "$" / "," )
@)? # userinfo
(?<host>
(?:[\pL\pN\pS-\.])+(?:\.?(?:[\pL]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
| # or
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address
| # or
\[
(?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}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:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
\] # a IPv6 address
)
(?:\:(?<port>[0-9]+))? # a port (optional)
(?<parameters>(?:;
(?<pname>(?:[a-z0-9\-_\.!\~*\'\(\)]|%[0-9a-f]{2}|[\[\]\/:&+$])+) # pname
(?:
=
(?<pvalue>(?:[a-z0-9\-_\.!\~*\'\(\)]|%[0-9a-f]{2}|[\[\]\/:&+$])+) # pvalue
)?
)*) #uri-parameters (optional)
(?<headers>\?
(?<header>
(?<hname>(?:[a-z0-9\-_\.!\~*\'\(\)]|%[0-9a-f]{2}|[\[\]\/?:\+$])+) # hname
=
(?<hvalue>(?:[a-z0-9\-_\.!\~*\'\(\)]|%[0-9a-f]{2}|[\[\]\/?:\+$])*) # hvalue
)(?:&(?P>header))*
)? #uri-headers (optional)
$~ixu';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment