3

greetings all I have a text that may contains emails and I want to detect any email occurrence and surround it with the < a > tag ex:

my.email@mycompany.com
<a href="mailto:my.email@mycompany.com"> my.email@mycompany.com </a>
codaddict
  • 445,704
  • 82
  • 492
  • 529
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • 3
    See http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses – McDowell Dec 30 '10 at 09:40

1 Answers1

1

Using the regex from regular-expression.info you can do:

text = text.replaceAll("(?i)\\b([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4})\\b",
                       "<a href=\"mailto:$1\"> $1 </a>");            

Ideone Link

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 1
    It should be noted that this expression will not pick up all valid e-mail addresses (e.g. `\@Joe\ Blogs@example.com`); whether you use it depends on how tolerant of errors you can be. – McDowell Dec 30 '10 at 10:48