正则表达没有回复电子邮件

时间:2015-10-12 20:29:07

标签: regex vb.net

是否有人知道我如何使用正则表达式来检查电子邮件地址是否有"无回复"或者"不回复"或" noreply"模式,以便自动避免向这些地址发送自动回复?

1 个答案:

答案 0 :(得分:1)

就个人而言,我发现正则表达式难以解读和维护(这是对我的编程能力的评论吗?)

如果您只想直接检查电子邮件地址是否包含字符串,则可能更容易阅读并维护此备选方案。重要的是这一行:

If emailAddress.ToUpper.Contains(noReplyString.ToUpper) Then

这只是将两个字符串转换为大写以进行比较,以便在两个字符串中混合的任何情况都变得无关紧要

我知道人们可能会将此标记为完全没有回答这个问题,但我必须尝试。它相当简单,但它的可读性和易于根据需要进行更改。 Form Load事件只是创建一个快速的代码来测试它。您可以将其传递到一个空的Windows窗体项目并运行它。在显示和解除两个消息框之后,项目的表单窗口将打开 - 就像代码编写的方式一样,为了快速,因为它已经很晚了:)

Public Class Form1
Dim noReplyList As New List (Of String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    initNoReplyList()
    If emailReplyStatus("noreplycompany@test.com") = True Then MessageBox.Show("REPLY!") Else MessageBox.Show("NOREPLY!")
    If emailReplyStatus("ggno-eplyd@test.com") = False Then MessageBox.Show(" NOREPLY!") Else MessageBox.Show("REPLY!")

End Sub

Private Sub initNoReplyList()
    ' add strings to search for here
    noReplyList.Add("no-reply")
    noReplyList.Add("do_not_reply")
    noReplyList.Add("noreply")
End Sub

Private Function emailReplyStatus(emailAddress As String) As Boolean
    ' presume a reply is ok unless found to be otherwise
    emailReplyStatus = True
    'cycles through each string in the list of noreply possibility comparing them in uppercase to
    'the email address in uppercase. 
    'If there is a match, exit set the email reply status to false and exit the for loop
    For Each noReplyString In noReplyList
        If emailAddress.ToUpper.Contains(noReplyString.ToString.ToUpper) Then
            emailReplyStatus = False
            Exit For
        End If
    Next
End Function