#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Laguna Niguel, CA - USA
    Posts
    121
    Rep Power
    12

    Best Mailto: hyperlink reg expression?


    Hello here,
    I am trying to parse any email contained in a text in order to create "mailto:" hyperlinks correctly.

    I am trying this regular expression which works great for regular emails, but not for emails with multiple dots int he domain name such as name@domain.co.uk

    PHP Code:
     $text preg_replace("/(\s|\>|^)([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})([\s|\.|\,|$|\<])/i","$1<a href=\"mailto:$2\">$2</a>$3"$text); 

    I have spent the entire day trying to figure out the correct regular expression with no luck!

    Any suggestions are very welcome!

    Thank you in advance to anyone.

    Best,
    Fab.
    Fabrizio Ferrari

    Virtual Sheet Music
    http://www.virtualsheetmusic.com
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Laguna Niguel, CA - USA
    Posts
    121
    Rep Power
    12
    Thank you, but even there I couldn't find a regular expression that matches most of the emails without tradeoffs... looks like it is impossible to find the "perfect" regex... thank you anyway!
    Fabrizio Ferrari

    Virtual Sheet Music
    http://www.virtualsheetmusic.com
  6. #4
  7. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    This is an ancient problem and there is indeed no "perfect" regex to match all emails, because TLDs are subject to change.

    There used to be a page where a guy had made "the perfect regex" to match emails. He had included all the existing TLDs (com, net, biz, info etc). Then more TLDs came out and he had updated it. It was a monster. I tried to google it but could find it no more---anyway, that's just to have a laugh, as you wouldn't want to use that sort of expression. Any email-matching regex is going to be a tradeoff.
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    I love when these threads pop up because then I can throw out my own monster regex.

    Originally Posted by ragax
    There used to be a page where a guy had made "the perfect regex" to match emails. He had included all the existing TLDs (com, net, biz, info etc). Then more TLDs came out and he had updated it. It was a monster. I tried to google it but could find it no more---anyway, that's just to have a laugh, as you wouldn't want to use that sort of expression. Any email-matching regex is going to be a tradeoff.
    This one, while mostly perfect, has a trade-off of being hella long and complicated. Oh, and it accepts things one might not realize are technically valid.
  10. #6
  11. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    That's a real beauty. Even the RB "translate to English" mode choked on it: "Tree truncated after 1000 nodes."
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Laguna Niguel, CA - USA
    Posts
    121
    Rep Power
    12
    Hey guys, I have found the following one among my old files, and seems to work fine:

    PHP Code:
    $text preg_replace("/(\s|\>|^)([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\.|\,])([a-z]{2,6})([a-zA-Z0-9\/\-\?\&\@\.\%\=\_\#]*)(\s|$|\<[b-zB-Z]+)/i""$1<a href=\"$2$3$4$5\">$2$3$4$5</a>$6"$text); 
    This regex works for URLs also

    Enjoy and thank you again for your help!
    Fabrizio Ferrari

    Virtual Sheet Music
    http://www.virtualsheetmusic.com

IMN logo majestic logo threadwatch logo seochat tools logo