Skip to content

Instantly share code, notes, and snippets.

@stilist
Last active May 3, 2016 15:27
Show Gist options
  • Save stilist/1dbfa6f7fba23fadb3b0 to your computer and use it in GitHub Desktop.
Save stilist/1dbfa6f7fba23fadb3b0 to your computer and use it in GitHub Desktop.
RFC 3966 regex
Copyright (c) 2016 Jordan Cole
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/tel:(\+[\d[\-\.\(\)]?]*\d[\d[\-\.\(\)]?]*(;[a-zA-Z\d\-]+(=([\[\]/:&\+\$]|[a-zA-Z\d[\-_\.!~\*'\(\)]]|%\h{2})+)?|;ext=[\d[\-\.\(\)]?]+|;isub=([;/\?:@&=\+\$,]|[a-zA-Z\d[\-_\.!~\*'\(\)]]|%\h{2})+)*|[\h\*#[\-\.\(\)]?][\h\*#]*[\h\*#[\-\.\(\)]?]*(;[a-zA-Z\d\-]+(=([\[\]/:&\+\$]|[a-zA-Z\d[\-_\.!~\*'\(\)]]|%\h{2})+)?|;ext=[\d[\-\.\(\)]?]+|;isub=([;/\?:@&=\+\$,]|[a-zA-Z\d[\-_\.!~\*'\(\)]]|%\h{2})+)*;phone-context=(([a-zA-Z\d]([a-zA-Z\d\-]*[a-zA-Z\d])?\.)*[a-zA-Z]([a-zA-Z\d\-]*[a-zA-Z\d])?\.?|\+[\d[\-\.\(\)]?]*\d[\d[\-\.\(\)]?]*)(;[a-zA-Z\d\-]+(=([\[\]/:&\+\$]|[a-zA-Z\d[\-_\.!~\*'\(\)]]|%\h{2})+)?|;ext=[\d[\-\.\(\)]?]+|;isub=([;/\?:@&=\+\$,]|[a-zA-Z\d[\-_\.!~\*'\(\)]]|%\h{2})+)*)/
#!/usr/bin/env ruby
# Builds an regular expression to parse RFC 3699 URIs.
#
# Expressions are broken up by the depth of embedded sub-expressions so it's
# easier to make sure everything is declared in the right order.
#
# @see https://tools.ietf.org/html/rfc3966#section-3
DEBUG = ENV['DEBUG'] || false
## 0: ABNF core
ALPHA = /a-zA-Z/
def test(regex, str, expected)
match = regex.match(str)
result = match ? match[0] : nil
puts "/#{regex.source}/ =~ '#{str}' => #{result}" if result != expected
end
## 1
# "-" / "_" / "." / "!" / "~" / "*" / "'" / "(" / ")"
mark = /[\-_\.!~\*'\(\)]/
if DEBUG
test(mark, '-', '-')
test(mark, '*', '*')
test(mark, 'z', nil)
test(mark, 'z!', '!')
end
# "[" / "]" / "/" / ":" / "&" / "+" / "$"
param_unreserved = /[\[\]\/:&\+\$]/
if DEBUG
test(param_unreserved, '[', '[')
test(param_unreserved, '+', '+')
test(param_unreserved, 'z', nil)
test(param_unreserved, 'z&', '&')
end
# "%" HEXDIG HEXDIG
pct_encoded = /%\h{2}/
if DEBUG
test(pct_encoded, '%aa', '%aa')
test(pct_encoded, '%AA', '%AA')
test(pct_encoded, '%55', '%55')
test(pct_encoded, '%a5', '%a5')
test(pct_encoded, '%A5', '%A5')
test(pct_encoded, '%5a', '%5a')
test(pct_encoded, '%5A', '%5A')
test(pct_encoded, '%5', nil)
test(pct_encoded, '%a', nil)
test(pct_encoded, '%555', '%55')
test(pct_encoded, '%aaa', '%aa')
test(pct_encoded, 'a%aaa', '%aa')
end
# ";" / "/" / "?" / ":" / "@" / "&" / "=" / "+" / "$" / ","
reserved = /[;\/\?:@&=\+\$,]/
if DEBUG
test(reserved, '?', '?')
test(reserved, '$', '$')
test(reserved, 'z', nil)
test(reserved, 'z@', '@')
end
# "-" / "." / "(" / ")"
visual_separator = /[\-\.\(\)]/
if DEBUG
test(visual_separator, '-', '-')
test(visual_separator, '(', '(')
test(visual_separator, ' ', nil)
end
## 2
# ALPHA / DIGIT
alphanum = /[#{ALPHA.source}\d]/
if DEBUG
test(alphanum, 'a', 'a')
test(alphanum, 'A', 'A')
test(alphanum, '5', '5')
test(alphanum, '-', nil)
end
# DIGIT / [ visual-separator ]
phonedigit = /[\d#{visual_separator.source}?]/
if DEBUG
test(phonedigit, '5', '5')
test(phonedigit, '-', '-')
test(phonedigit, 'z', nil)
test(phonedigit, 'z5', '5')
end
# HEXDIG / "*" / "#" / [ visual-separator ]
phonedigit_hex = /[\h\*##{visual_separator.source}?]/
if DEBUG
test(phonedigit_hex, '5', '5')
test(phonedigit_hex, 'f', 'f')
test(phonedigit_hex, '*', '*')
test(phonedigit_hex, '#', '#')
test(phonedigit_hex, '-', '-')
test(phonedigit_hex, 'z', nil)
test(phonedigit_hex, 'z5a', '5')
test(phonedigit_hex, 'f-', 'f')
end
## 3
# alphanum / alphanum *( alphanum / "-" ) alphanum
domainlabel = /#{alphanum.source}([#{ALPHA.source}\d\-]*#{alphanum.source})?/
if DEBUG
test(domainlabel, 'a', 'a')
test(domainlabel, 'z', 'z')
test(domainlabel, '5', '5')
test(domainlabel, '5a', '5a')
test(domainlabel, '5-a', '5-a')
test(domainlabel, '5aa', '5aa')
test(domainlabel, '5aaaaaaa', '5aaaaaaa')
test(domainlabel, '5-', '5')
test(domainlabel, '#', nil)
test(domainlabel, '5a-', '5a')
test(domainlabel, '#z', 'z')
test(domainlabel, '#za', 'za')
test(domainlabel, '#z-', 'z')
test(domainlabel, '#z-a', 'z-a')
end
# ";ext=" 1*phonedigit
extension = /;ext=#{phonedigit.source}+/
if DEBUG
test(extension, ';ext=5', ';ext=5')
test(extension, ';ext=555', ';ext=555')
test(extension, ';ext=5-', ';ext=5-')
test(extension, ';ext=-', ';ext=-')
test(extension, ';ext=', nil)
test(extension, ';ext=a', nil)
test(extension, ';ext=5a', ';ext=5')
end
# "+" *phonedigit DIGIT *phonedigit
global_number_digits = /\+#{phonedigit.source}*\d#{phonedigit.source}*/
if DEBUG
test(global_number_digits, '+5', '+5')
test(global_number_digits, '+-5', '+-5')
test(global_number_digits, '+5-5', '+5-5')
test(global_number_digits, '+5-555555555', '+5-555555555')
test(global_number_digits, '+555555555-5', '+555555555-5')
test(global_number_digits, '+555555555-555555555', '+555555555-555555555')
test(global_number_digits, '+555555555-555555555-', '+555555555-555555555-')
test(global_number_digits, '+', nil)
test(global_number_digits, '+-', nil)
end
# *phonedigit-hex (HEXDIG / "*" / "#")*phonedigit-hex
# @note This regex may be wrong--I'm not sure how to intepret the `(rule)*rule`
# syntax.
local_number_digits = /#{phonedigit_hex.source}[\h\*#]*#{phonedigit_hex.source}*/
if DEBUG
test(local_number_digits, '5', '5')
test(local_number_digits, '-', '-')
test(local_number_digits, '5555', '5555')
test(local_number_digits, '55-5', '55-5')
test(local_number_digits, '55-5', '55-5')
test(local_number_digits, '5f', '5f')
test(local_number_digits, '5f5', '5f5')
test(local_number_digits, '5*5', '5*5')
test(local_number_digits, '5#5', '5#5')
test(local_number_digits, 'z', nil)
test(local_number_digits, 'z5', '5')
end
# 1*( alphanum / "-" )
pname = /[#{ALPHA.source}\d\-]+/
if DEBUG
test(pname, 'a', 'a')
test(pname, 'z', 'z')
test(pname, '5', '5')
test(pname, '-', '-')
test(pname, '-z', '-z')
test(pname, 'z-', 'z-')
test(pname, 'aaa', 'aaa')
test(pname, '?', nil)
end
# ALPHA / ALPHA *( alphanum / "-" ) alphanum
toplabel = /[#{ALPHA.source}]([#{ALPHA.source}\d\-]*#{alphanum.source})?/
if DEBUG
test(toplabel, 'a', 'a')
test(toplabel, 'aa', 'aa')
test(toplabel, 'aaa', 'aaa')
test(toplabel, 'a5a', 'a5a')
test(toplabel, 'aa5', 'aa5')
test(toplabel, 'a-5', 'a-5')
test(toplabel, '5', nil)
test(toplabel, '5!', nil)
test(toplabel, '5-', nil)
test(toplabel, '5-@', nil)
test(toplabel, 'a@', 'a')
test(toplabel, '@a', 'a')
end
# alphanum / mark
unreserved = /[#{ALPHA.source}\d#{mark.source}]/
if DEBUG
test(unreserved, 'a', 'a')
test(unreserved, 'z', 'z')
test(unreserved, '5', '5')
test(unreserved, '*', '*')
test(unreserved, '?', nil)
test(unreserved, '?a', 'a')
end
## 4
# *( domainlabel "." ) toplabel [ "." ]
domainname = /(#{domainlabel.source}\.)*#{toplabel.source}\.?/
if DEBUG
test(domainname, 'a', 'a')
test(domainname, 'a.a', 'a.a')
test(domainname, '5.a', '5.a')
test(domainname, 'a.a.', 'a.a.')
test(domainname, '5.a.', '5.a.')
test(domainname, 'a.a-a', 'a.a-a')
test(domainname, 'a.a-a.', 'a.a-a.')
test(domainname, 'a.a-a.a-a', 'a.a-a.a-a')
test(domainname, 'a.5-a.a-a', 'a.5-a.a-a')
test(domainname, 'a.a-a.a-a.', 'a.a-a.a-a.')
test(domainname, 'a.5-a.a-a.', 'a.5-a.a-a.')
test(domainname, '5', nil)
test(domainname, '5.', nil)
test(domainname, '5.a', '5.a')
test(domainname, '5.a.', '5.a.')
test(domainname, 'a.5', 'a.')
test(domainname, 'a.5.', 'a.')
test(domainname, 'a.5-a', 'a.')
test(domainname, 'a.5-a.', 'a.')
test(domainname, 'a.5-a.5-a', 'a.')
test(domainname, 'a.5-a.5-a.', 'a.')
end
# param-unreserved / unreserved / pct-encoded
paramchar = /(#{param_unreserved.source}|#{unreserved.source}|#{pct_encoded.source})/
if DEBUG
test(paramchar, '[', '[')
test(paramchar, 'a', 'a')
test(paramchar, '5', '5')
test(paramchar, '!', '!')
test(paramchar, '%f5', '%f5')
test(paramchar, '%', nil)
test(paramchar, '%a', 'a')
test(paramchar, 'a%', 'a')
test(paramchar, '%az', 'a')
end
# reserved / unreserved / pct-encoded
uric = /(#{reserved.source}|#{unreserved.source}|#{pct_encoded.source})/
if DEBUG
test(uric, '/', '/')
test(uric, 'a', 'a')
test(uric, '5', '5')
test(uric, '%f5', '%f5')
test(uric, '#', nil)
test(uric, '#a', 'a')
test(uric, 'a#', 'a')
end
## 5
# domainname / global-number-digits
descriptor = /(#{domainname.source}|#{global_number_digits.source})/
if DEBUG
test(descriptor, 'a', 'a')
test(descriptor, 'a.a', 'a.a')
test(descriptor, '+5', '+5')
test(descriptor, '+5-555555555', '+5-555555555')
test(descriptor, '5', nil)
test(descriptor, 'a+5', 'a')
test(descriptor, '+5a', '+5')
test(descriptor, '+5a', '+5')
end
# 1*paramchar
pvalue = /#{paramchar.source}+/
if DEBUG
test(pvalue, 'a', 'a')
test(pvalue, '5', '5')
test(pvalue, '!', '!')
test(pvalue, 'aaaaaaaa', 'aaaaaaaa')
test(pvalue, '%', nil)
test(pvalue, '%a', 'a')
end
## 6
# ";phone-context=" descriptor
context = /;phone-context=#{descriptor.source}/
if DEBUG
test(context, ';phone-context=a', ';phone-context=a')
test(context, ';phone-context=a.a', ';phone-context=a.a')
test(context, ';phone-context=+5', ';phone-context=+5')
test(context, ';phone-context=+5-555555555', ';phone-context=+5-555555555')
test(context, ';phone-context=', nil)
test(context, ';phone-context=@', nil)
end
# ";isub=" 1*uric
isdn_subaddress = /;isub=#{uric.source}+/
if DEBUG
test(isdn_subaddress, ';isub=a', ';isub=a')
test(isdn_subaddress, ';isub=5', ';isub=5')
test(isdn_subaddress, ';isub=%f5', ';isub=%f5')
test(isdn_subaddress, ';isub=', nil)
test(isdn_subaddress, ';isub=#', nil)
end
# ";" pname ["=" pvalue ]
parameter = /;#{pname.source}(=#{pvalue.source})?/
if DEBUG
test(parameter, ';a', ';a')
test(parameter, ';5', ';5')
test(parameter, ';a=b', ';a=b')
test(parameter, ';', nil)
test(parameter, ';%', nil)
test(parameter, ';a=', ';a')
end
## 7
# parameter / extension / isdn-subaddress
par = /(#{parameter.source}|#{extension.source}|#{isdn_subaddress.source})/
if DEBUG
test(par, ';a', ';a')
test(par, ';ext=5', ';ext=5')
test(par, ';isub=5', ';isub=5')
test(par, ';', nil)
test(par, ';%', nil)
test(par, ';ext=%', ';ext')
test(par, ';isub=%', ';isub')
end
## 8
# global-number-digits *par
global_number = /#{global_number_digits.source}#{par.source}*/
if DEBUG
test(global_number, '+5', '+5')
test(global_number, '+5-555-555-5555', '+5-555-555-5555')
test(global_number, '+5;a', '+5;a')
test(global_number, '+5-5;a', '+5-5;a')
test(global_number, '+5-5;isub', '+5-5;isub')
test(global_number, '+5-5;isub;ext=5', '+5-5;isub;ext=5')
test(global_number, '+5-555-555-5555;isub;ext=5', '+5-555-555-5555;isub;ext=5')
test(global_number, '+a', nil)
test(global_number, '+5;', '+5')
test(global_number, 'a+5;', '+5')
end
# local-number-digits *par context *par
local_number = /#{local_number_digits.source}#{par.source}*#{context.source}#{par.source}*/
if DEBUG
test(local_number, '5;phone-context=a', '5;phone-context=a')
test(local_number, '55-55;phone-context=a', '55-55;phone-context=a')
test(local_number, '5;a=5;phone-context=a', '5;a=5;phone-context=a')
test(local_number, '55-55;a=5;phone-context=a', '55-55;a=5;phone-context=a')
test(local_number, '5;phone-context=a;a=5', '5;phone-context=a;a=5')
test(local_number, '55-55;phone-context=a;a=5', '55-55;phone-context=a;a=5')
test(local_number, '5;a=5;phone-context=a;z=5', '5;a=5;phone-context=a;z=5')
test(local_number, '55-55;a=5;phone-context=a;z=5', '55-55;a=5;phone-context=a;z=5')
test(local_number, '5', nil)
test(local_number, '55-55', nil)
test(local_number, '5;', nil)
test(local_number, '55-55;', nil)
end
## 9
# global-number / local-number
telephone_subscriber = /(#{global_number.source}|#{local_number.source})/
if DEBUG
test(telephone_subscriber, '5;phone-context=a', '5;phone-context=a')
test(telephone_subscriber, '+55555555555;ext=5', '+55555555555;ext=5')
test(telephone_subscriber, '+5-555-555-5555;isub;ext=5', '+5-555-555-5555;isub;ext=5')
end
## 10
# "tel:" telephone-subscriber
telephone_uri = /tel:#{telephone_subscriber.source}/
if DEBUG
test(telephone_uri, 'tel:+55555555555', 'tel:+55555555555')
test(telephone_uri, 'tel:+5-555-555-5555', 'tel:+5-555-555-5555')
test(telephone_uri, 'tel:+55555555555;ext=5', 'tel:+55555555555;ext=5')
test(telephone_uri, 'tel:+5-555-555-5555;isub=5', 'tel:+5-555-555-5555;isub=5')
test(telephone_uri, 'tel:5555;phone-context=example.com', 'tel:5555;phone-context=example.com')
test(telephone_uri, 'tel:555-5555;phone-context=+5-555-555', 'tel:555-5555;phone-context=+5-555-555')
test(telephone_uri, 'tel:+5', 'tel:+5')
test(telephone_uri, 'tel:a', nil)
test(telephone_uri, 'tel:(555) 555-555', nil)
end
puts telephone_uri.source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment