locked
someone@yoursite.com --> <a href="mailto:someone@yoursite.com">someone@yoursite.com</a> RRS feed

  • Question

  • User-502227870 posted

    Hello all, I have been trying to find a solution to replace all email addresses in a text string to mailto links...


       Public Shared Function ConvertEmailToMailToLinks(ByVal textToconvert As String) As String
    
            Dim pattern As String = "\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"
    
            Dim mailtoemail As New StringBuilder
            mailtoemail.Append("<a href='mailto://")
            mailtoemail.Append(pattern)
            mailtoemail.Append("'>")
            mailtoemail.Append(pattern)
            mailtoemail.Append("</a>")
    
    
            Dim input As String = textToconvert
            Dim rgx As New Regex(pattern)
            Dim result As String = rgx.Replace(input, mailtoemail.ToString)
    
            Return result
    
        End Function



    Friday, October 22, 2010 11:50 AM

Answers

  • User-502227870 posted

    I figured it out...here is how I did this:


        Public Shared Function ConvertEmailToMailToLinks(ByVal textToconvert As String) As String
    
    
            Dim emailRegex As Regex = New Regex("([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", _
                                      RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)
            textToconvert = emailRegex.Replace(textToconvert, "<a href='mailto:$1@$2$6'>$1@$2$6</a>")
            Return textToconvert
    
        End Function



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 25, 2010 9:44 AM

All replies

  • User-502227870 posted

    I tried this too, but it doesn't work either...


                Dim emailRegex As Regex = New Regex("\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)
    
                lblText.Text = "COMMUNICATIONS: Joe Blow (BlowJoe@joeblow.com) fredflinstone@gmail.com"
    
                lblText.Text = emailRegex.Replace(lblText.Text, "<a href='mailto:$(1)'>$(1)</a>")




    Friday, October 22, 2010 12:18 PM
  • User-502227870 posted

    This is my latest attempt which doesn't work either:


                lblText.Text = "COMMUNICATIONS: Joe Blow (BlowJoe@joeblow.com) fredflinstone@gmail.com"
                Dim emailRegex As Regex = New Regex _
                                          ("\b                                                     " & _
                                          "(?# Capture the address to $1...)                       " & _
                                          "(                                                       " & _
                                          "    \w[-.\w]*                          (?# usernane)    " & _
                                          "    @                                                   " & _
                                          "    [-\w]+(\.[-\w]+)*\.(com|edu|info)  (?# hostname)    " & _
                                          ")                                                       " & _
                                          "\b                                                      ", _
                                          RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)
    
                lblText.Text = emailRegex.Replace(lblText.Text, "<a href='mailto:$(1)'>$(1)</a>")



    Friday, October 22, 2010 12:57 PM
  • User-502227870 posted

    I figured it out...here is how I did this:


        Public Shared Function ConvertEmailToMailToLinks(ByVal textToconvert As String) As String
    
    
            Dim emailRegex As Regex = New Regex("([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", _
                                      RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)
            textToconvert = emailRegex.Replace(textToconvert, "<a href='mailto:$1@$2$6'>$1@$2$6</a>")
            Return textToconvert
    
        End Function



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 25, 2010 9:44 AM