[openstreetmap/openstreetmap-website] Show mailto: links for an email or emails separated by ; (#2542)

Boubacar notifications at github.com
Wed Feb 19 11:42:16 UTC 2020

I didn't think of huge regexps counting as lots of code before, but yes using `URI::MailTo::EMAIL_REGEXP` definitely makes a lot more sense here. Thanks for bringing it up,  I don't know how I didn't come across it before. (Side Note: Interestingly enough, it actually currently uses the same exact implementation for the [expression matching an email](https://github.com/ruby/ruby/blob/master/lib/uri/mailto.rb#L56)).

The problem with using it in a semi-colon delineated approach is that it contains the modifiers `\A` and `\z` at the beginning and end respectively, which means it only matches single emails with no trailing whitespace at either end. Building around the regexp using string interpolation directly (i.e using  `#{URI::MailTo::EMAIL_REGEXP}` within the match regex) doesn't work because of this.

I am considering two approaches to dealing with this so semi-colon delineated lists of emails (with possible whitespace around the semicolons) are matched:

* My first thought was to manipulate the built-in regexp literal by converting it to a string and using substring indexing so the `\A` and `\z` are just not included.  The full regex would look like:
`\A\s*(?:#{URI::MailTo::EMAIL_REGEXP.to_s[9..-4]})\s*` \
This feels very hacky and may not be much easier to read, though. 

* Another possible approach would be splitting the value on ';' first, then stripping each possible email and checking if it matches the built-in regexp directly. In the loop, nil would be returned right away if an unmatched email occurs. The only issue I see with this approach is the fact that it may do some unnecessary work when given a semi-colon delineated list containing an invalid email towards the end, since it iterates through the array created by .split() before checking validity.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20200219/b1734993/attachment.htm>

More information about the rails-dev mailing list