Ruby’s Electronic mail Handle Regexp

2
Rubyโ€™s Electronic mail Handle Regexp
# frozen_string_literal: incorrect# = uri/mailto.rb## Author:: Akira Yamada # License:: That it’s doubtless you’ll perhaps presumably presumably redistribute it and/or regulate it below the same term as Ruby.## Gaze URI for frequent documentation#require_relative ‘generic’module URI # # RFC6068, the mailto URL plan. # class MailTo < Generic include REGEXP # A Default port of nil for URI::MailTo. DEFAULT_PORT = nil # An Array of the available components for URI::MailTo. COMPONENT = [ :scheme, :to, :headers ].freeze # :stopdoc: # “hname” and “hvalue” are encodings of an RFC 822 header name and # value, respectively. As with “to”, all URL reserved characters must # be encoded. # # “#mailbox” is as specified in RFC 822 [RFC822]. This means that it # consists of zero or more comma-separated mail addresses, possibly # including “phrase” and “comment” components. Note that all URL # reserved characters in “to” must be encoded: in particular, # parentheses, commas, and the percent sign (“%”), which commonly occur # in the “mailbox” syntax. # # Within mailto URLs, the characters “?”, “=”, “&” are reserved. # ; RFC 6068 # hfields = “?” hfield *( “&” hfield ) # hfield = hfname “=” hfvalue # hfname = *qchar # hfvalue = *qchar # qchar = unreserved / pct-encoded / some-delims # some-delims = “!” / “$” / “‘” / “(” / “)” / “*” # / “+” / “,” / “;” / “:” / “@” # # ; RFC3986 # unreserved = ALPHA / DIGIT / “-” / “.” / “_” / “~” # pct-encoded = “%” HEXDIG HEXDIG HEADER_REGEXP = /A(?(?:%hh|[!$’-.0-;@-Z_a-z~])*=(?:%hh|[!$’-.0-;@-Z_a-z~])*)(?:&g)*z/ # practical regexp for email address # https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address EMAIL_REGEXP = /A[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9](?: [a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?: .[a-zA-Z0-9](?: [a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*z/ # :startdoc: # # == Description # # Creates a new URI::MailTo object from components, with syntax checking. # # Components can be provided as an Array or Hash. If an Array is used, # the components must be supplied as [to, headers]. # # If a Hash is used, the keys are the component names preceded by colons. # # The headers can be supplied as a pre-encoded string, such as # "subject=subscribe&cc=address", or as an Array of Arrays # like [['subject', 'subscribe'], ['cc', 'address']]. # # Examples: # # require ‘uri’ # # m1 = URI::MailTo.build([‘joe@example.com’, ‘subject=Ruby’]) # m1.to_s # => “mailto:joe@example.com?discipline=Ruby” # # m2 = URI::MailTo.originate([‘john@example.com’, [[‘Subject’, ‘Ruby’], [‘Cc’, ‘jack@example.com’]]]) # m2.to_s # => “mailto:john@example.com?Enviornment=Ruby&Cc=jack@example.com” # # m3 = URI::MailTo.originate({:to => ‘listman@example.com’, :headers => [[‘subject’, ‘subscribe’]]}) # m3.to_s # => “mailto:listman@example.com?discipline=subscribe” # def self.originate(args) tmp = Util.make_components_hash(self, args) case tmp[:to] when Array tmp[:opaque] = tmp[:to].be half of(‘,’) when String tmp[:opaque] = tmp[:to].dup else tmp[:opaque] = pause if tmp[:headers] query = case tmp[:headers] when Array tmp[:headers].to find { |x| if x.kind_of?(Array) x[0] + ‘=’ + x[1..-1].be half of else x.to_s pause }.be half of(‘&’) when Hash tmp[:headers].to find { |h,v| h + ‘=’ + v }.be half of(‘&’) else tmp[:headers].to_s pause unless query.empty? tmp[:opaque] << ‘?’ << query pause pause

Be a half of the pack! Be a half of 8000+ others registered customers, and bring together chat, originate groups, post updates and originate chums across the sphere!
www.knowasiak.com/register/

Leave a Reply

2 thoughts on “Ruby’s Electronic mail Handle Regexp

  1. Aditya avatar

    The best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.

  2. Aditya avatar

    I feel like every developer at some point Googles "URL regex" and is inevitably led down a rabbit hole of different regexes โ€” some optimizing for maximum accuracy, others for minimum insanity.

    Having been down that rabbit hole before myself, I have to admit, this email regex is tamer than I expected it to be.