Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式提取电子邮件_Regex_Excel_Vba - Fatal编程技术网

Regex 正则表达式提取电子邮件

Regex 正则表达式提取电子邮件,regex,excel,vba,Regex,Excel,Vba,我只需要从Excel中的电子表格中提取电子邮件。我在这里的StackOverflow找到了一些VBA代码示例,由提供 我创建了一个Excel模块,它似乎工作正常,只是它只将地址的第一个大写字符返回到单元格中,而忽略了电子邮件 例如: Text | Result ----------------------------------------|------------------------------ My email add

我只需要从Excel中的电子表格中提取电子邮件。我在这里的StackOverflow找到了一些VBA代码示例,由提供

我创建了一个Excel模块,它似乎工作正常,只是它只将地址的第一个大写字符返回到单元格中,而忽略了电子邮件

例如:

Text                                    | Result
----------------------------------------|------------------------------
My email address is address@gmail.com   | My email address is  
Yes  Address@gmail.com                  | Yes  A
下面是我正在使用的代码:

Function simpleCellRegex(Myrange As Range) As String
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim strOutput As String


    strPattern = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"

    If strPattern <> "" Then
        strInput = Myrange.Value
        strReplace = ""

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.test(strInput) Then
            simpleCellRegex = regEx.Replace(strInput, strReplace)
        Else
            simpleCellRegex = "Not matched"
        End If
    End If
End Function
函数simpleCellRegex(Myrange作为范围)作为字符串
Dim regEx作为新的RegExp
作为字符串的Dim strPattern
像弦一样的模糊的条纹
变暗字符串替换为字符串
作为字符串的暗输出
strPattern=“[a-z0-9!#$%&'*+/=?^{{124}-]+(?:\.[a-z0-9!{35;$%&'*+/=?^{124}-]+*([a-z0-9](?:[a-z0-9-]*[a-z0-9])+[a-z0-9]+[a-z0-9](?:[a-z0-z0-9-]
如果strPattern“”则
strInput=Myrange.Value
strReplace=“”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
simpleCellRegex=regEx.Replace(strInput,strReplace)
其他的
simpleCellRegex=“不匹配”
如果结束
如果结束
端函数
我没有足够的VBA经验来真正诊断这里可能发生的事情,希望有人能够发现我做错了什么

工作代码
函数simpleCellRegex(Myrange作为范围)作为字符串
Dim regEx作为新的RegExp
作为字符串的Dim strPattern
像弦一样的模糊的条纹
变暗字符串替换为字符串
作为字符串的暗输出
strPattern=“[A-Za-z0-9!#$%&'*+/=?^{{124}-]+(?:\.[A-z0-9!#$%&'*+/=?^{124}-]+)*([A-z0-9](?:[A-Za-z0-9-]*[A-z0-9])+[A-z0-9](?:[A-z0-9-]
如果strPattern“”则
strInput=Myrange.Value
strReplace=“”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=True
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
Set matches=regEx.Execute(strInput)
simpleCellRegex=匹配项(0).Value
其他的
simpleCellRegex=“不匹配”
如果结束
如果结束
端函数
尝试以下模式

strPattern  = "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"

你可以换线

 simpleCellRegex = regEx.Replace(strInput, strReplace)


因为在返回strInput时没有进行任何替换,所以只会得到与输入相同的字符串。 您需要返回使用RegExp找到的值

试一试

而不是

simpleCellRegex = regEx.Replace(strInput, strReplace)

最简单的方法是安装名为KUtool的软件。安装后,突出显示要提取电子邮件的内容==>单击顶部中间的ku工具==>单击文本==>提取电子邮件。 您还可以使用以下代码。(ALT+F1==>插入模块)

您也可以按照代码的方式进行 打开excell,单击ALT+F1,单击insert Module并粘贴此代码


单击“保存”并在空白单元格中输入公式(列=ExtractEmailFun(A1))。按enter键,您的电子邮件将被提取。如果您不在正则表达式模式掩码中同时指定大写和小写,但希望捕获不区分大小写的结果,希望这会有所帮助。IgnoreCase=False不应该是
。IgnoreCase=True
?相关:您所说的有意义,但是更改
.IgnoreCase=False
对结果没有影响。@pnuts我已经在代码中将
.IgnoreCase
更改为true。我已经注意到,这实际上是工作,但向后!代码提取除电子邮件以外的所有内容。我们如何使代码反向工作呢?我是这样想的,但本质上这只是复制单元格的值。它没有提取地址Thank you@Marcin Wesel这应该有效,但我得到了一个#值!“我的单元格中有错误,无法找到正确匹配的电子邮件。”。可能是格式问题吗?我发现它正在查找与代码匹配的第二个实例。将
匹配项(1)更改为
匹配项(0)。值

 simpleCellRegex = strInput
Set matches = regEx.Execute(strInput)
simpleCellRegex = matches(1).Value
simpleCellRegex = regEx.Replace(strInput, strReplace)
Function ExtractEmailFun(extractStr As String) As String
'Update 20130829
Dim CharList As String
On Error Resume Next
CheckStr = "[A-Za-z0-9._-]"
OutStr = ""
Index = 1
Do While True
    Index1 = VBA.InStr(Index, extractStr, "@")
    getStr = ""
    If Index1 > 0 Then
        For p = Index1 - 1 To 1 Step -1
            If Mid(extractStr, p, 1) Like CheckStr Then
                getStr = Mid(extractStr, p, 1) & getStr
            Else
                Exit For
            End If
        Next
        getStr = getStr & "@"
        For p = Index1 + 1 To Len(extractStr)
            If Mid(extractStr, p, 1) Like CheckStr Then
                getStr = getStr & Mid(extractStr, p, 1)
            Else
                Exit For
            End If
        Next
        Index = Index1 + 1
        If OutStr = "" Then
            OutStr = getStr
        Else
            OutStr = OutStr & Chr(10) & getStr
        End If
    Else
        Exit Do
    End Ifenter code here
Loop
ExtractEmailFun = OutStr
End Function