Detect emails in text and pass it tag

greetings of all I have text that can contain emails and I want to detect any occurrence of email and surround it with a tag < a > for example:

my.email@mycompany.com
<a href="mailto:my.email@mycompany.com"> my.email@mycompany.com </a>
+3
source share
1 answer

Using the regular expression 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>");            

Perfect link

+1
source

Source: https://habr.com/ru/post/1782743/


All Articles