Strip everything but an email address from a text box
I've a text box in an Access Db that the user should enter an email address into. This is done by copying from Outlook 2010. Unfortunately, that copy also brings the Name and angular brackets, Fred Bloggs <fred@bloggs.co.uk>
I was hoping to use this regex
.pattern = "[\w-\.]{开发者_如何学运维1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}"
to identify the email address.
How would I strip everything else and throw an error if no valid email address is entered?
Microsoft Access VBA does not have a built-in Regex engine. You can, however, add a reference to the VBScript Regular Expression Library and use that one.
Here's a related question that explains how to do that:
- Regular Expressions in MS Access VBA?
If the email is always enclosed in the final <>
you could
Public Function fmt(email As String) As String
pos = InStrRev(email, "<")
If (pos > 0) Then
email = Mid$(email, 1 + pos, 1 + Len(email) - pos)
email = Left$(email, Len(email) - 1)
End If
fmt = email
End Function
or
replace(mid(email,instrrev(email,"<")+1,len(email)),">","")
Edit;
For a regexp check add a reference to the "Microsoft VBScript Regular Expressions library" (tools > references) and;
Public Function fmt(email As String) As String
pos = InStrRev(email, "<")
If (pos > 0) Then
email = Mid$(email, 1 + pos, 1 + Len(email) - pos)
email = Left$(email, Len(email) - 1)
End If
fmt = email
With New RegExp
.Global = True
.IgnoreCase = True
.MultiLine = True
.Pattern = "^\S+@\S+\.\S+$"
If Not .Test(fmt) Then fmt = ""
End With
End Function
This returns a valid email address, or "" if its not valid.
I dropped your RE; reasoning: Using a regular expression to validate an email address
精彩评论