Check if string is an email

Hi, I am using a code I copied from another website

[code]Dim emailAddress As String = "test@email.com "

Dim re As RegEx
re = New RegEx
Dim rm As RegExMatch

re.SearchPattern = “[a-z0-9!#$%&’+/=?^_{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)@(?:a-z0-9?\.)+a-z0-9?”
rm = re.Search(emailAddress)

if rm = Nil Then
msgbox “false”
Else
msgbox “true”
End if[/code]

Does anyone know why it returns true for this email ? Note the empty char at the end.

[quote=134386:@Walter Sander]Hi, I am using a code I copied from another website

[code]Dim emailAddress As String = "test@email.com "

Dim re As RegEx
re = New RegEx
Dim rm As RegExMatch

re.SearchPattern = “[a-z0-9!#$%&’+/=?^_{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)@(?:a-z0-9?\.)+a-z0-9?”
rm = re.Search(emailAddress)

if rm = Nil Then
msgbox “false”
Else
msgbox “true”
End if[/code]

Does anyone know why it returns true for this email ? Note the empty char at the end.[/quote]

if you are running on a Mac, RegExEx has great samples on detecting email addresses and such in it. Otherwise I would chat with @Kem Tekinay …

The string contains a valid email address. If you want to verify that the string is a valid email, try this:
re.SearchPattern = “^[a-z0-9!#$%&’+/=?^_{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)@(?:a-z0-9?\.)+a-z0-9?$”

1 Like

@Mark Walsh thanks, that one seems to work okay :slight_smile:

@scott boss nop, I am using Windows.

still reach out to @Kem Tekinay as he is the RegEx guru.

FYI, there is also a Windows version of RegExRX, available through my website.