Search code examples
asp-classic

ASP Classic extract email address from string


I need to be able to pull an email address from a string in ASP Classic.

Example string is "Please Contact Cp. from XXXX located in xxxx with regards to enquiry details - Hi, I am writing to you to cancel Thank you . Chris Pearson email address : chris@itsanemail.com.au phone number : 07 56431545 Preferred contact method : Email" With the result being "chris@itsanemail.com.au"

note that the email wont always be in the same location within the string. there will however always be spaces either side.

i can do this in excel without issue, but cant find a script to do this. it doesn't have to be perfect. happy if it picks up the majority


Solution

  • Function GetEmailAddrFromStr(TheString)
        If InStr(1,TheString,"@") > 0 Then
            TheAt = InStr(1,TheString,"@")
            StartMarker = InstrRev(TheString, " ", TheAt, 0)
            EndMarker = InStr(TheAt,TheString," ")
            GetEmailAddrFromStr = Trim(Mid(TheString,StartMarker,EndMarker-StartMarker))
        Else
            GetEmailAddrFromStr = ""
        End If
    End Function
    
    SomeText = "Hi, I am writing to you to cancel Thank you . Chris Pearson email address : chris@itsanemail.com.au phone number : 07 56431545 Preferred contact method : Email"
    
    Response.write GetEmailAddrFromStr(SomeText)