Skip to content

Instantly share code, notes, and snippets.

@dpk dpk/gist:4757681

Last active Jan 29, 2016
Embed
What would you like to do?
RFC 3986 URI BNF translated to a regular expression. (Uses Ruby/Oniguruma/Onigmo named capture syntax)
(?xi)
\A
# unreserved: [a-z0-9\-._~]
# sub-delims: [!$&'()*+,;=]
# pct-encoded: %[0-9a-f]{2}
# dec-octet: (?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
# pchar: (?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})
# h16: [0-9a-f]{1,4}
# ls32: (?:[0-9a-f]{1,4}:[0-9a-f]{4}|
# (?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
# \.
# (?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
# \.
# (?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
# \.
# (?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))
# segment: (?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*
# segment-nz: (?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+
(?<scheme>[a-z][a-z0-9+\-.]*):
(?<hier-part>
(?:
//(?<authority>
(?:(?:(?<userinfo>(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*)@)?)
(?<host>
(?<IP-literal>\[(?:
(?<IPv6address>
# OHSHI-
(?:[0-9a-f]{1,4}){6}(?:[0-9a-f]{1,4}:[0-9a-f]{4}|(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))
|
::(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ty> (?:/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)
|
(?<path-absolute>
/(?:
(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+
(?:/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*
)?
)
|
(?<path-rootless>
(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+
(?:/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*
)
|
(?<path-empty>)
)
)
(?:\?(?<query> (?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*))?
(?:\#(?<fragment> (?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*))?
\Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.