MatchesRfc8089Path🔗
Check that
text is a path conforming to the pattern of RFC 8089.
The definition has been taken from:
https://datatracker.ietf.org/doc/html/rfc8089
Code
h16 = "[0-9A-Fa-f]{1,4}"
dec_octet = "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
ipv4address = f"{dec_octet}\\.{dec_octet}\\.{dec_octet}\\.{dec_octet}"
ls32 = f"({h16}:{h16}|{ipv4address})"
ipv6address = (
f"(({h16}:){{6}}{ls32}|::({h16}:){{5}}{ls32}|({h16})?::({h16}:){{4}}"
f"{ls32}|(({h16}:)?{h16})?::({h16}:){{3}}{ls32}|(({h16}:){{2}}{h16})?::"
f"({h16}:){{2}}{ls32}|(({h16}:){{3}}{h16})?::{h16}:{ls32}|(({h16}:){{4}}"
f"{h16})?::{ls32}|(({h16}:){{5}}{h16})?::{h16}|(({h16}:){{6}}{h16})?::)"
)
unreserved = "[a-zA-Z0-9\\-._~]"
sub_delims = "[!$&'()*+,;=]"
ipvfuture = f"[vV][0-9A-Fa-f]+\\.({unreserved}|{sub_delims}|:)+"
ip_literal = f"\\[({ipv6address}|{ipvfuture})\\]"
pct_encoded = "%[0-9A-Fa-f][0-9A-Fa-f]"
reg_name = f"({unreserved}|{pct_encoded}|{sub_delims})*"
host = f"({ip_literal}|{ipv4address}|{reg_name})"
file_auth = f"(localhost|{host})"
pchar = f"({unreserved}|{pct_encoded}|{sub_delims}|[:@])"
segment_nz = f"({pchar})+"
segment = f"({pchar})*"
path_absolute = f"/({segment_nz}(/{segment})*)?"
auth_path = f"({file_auth})?{path_absolute}"
local_path = f"{path_absolute}"
file_hier_part = f"(//{auth_path}|{local_path})"
file_scheme = "file"
file_uri = f"{file_scheme}:{file_hier_part}"
pattern = f"^{file_uri}$"
return match(pattern, text) is not None