回 帖 发 新 帖 刷新版面

主题:[原创]【算法帖】从文本提取邮箱地址的方法

最新研究结果……
Function GetEmail(ByVal TemTxt As String)
Dim Pointer As Long
Dim TopIndex As Long, DownIndex As Long
PoinTer = InStr(1, tmpTxt, "@")
If PoinTer = 0 Then Exit Sub
Do While PoinTer <> 0
    TopIndex = PoinTer
    DownIndex = PoinTer
    For i = PoinTer To 1 Step -1
        AscCode = Asc(Mid(tmpTxt, i, 1))
        If AscCode = 46 Or (AscCode >= 48 And AscCode <= 57) Or (AscCode >= 64 And AscCode <= 90) Or AscCode = 95 Or (AscCode >= 97 And AscCode <= 122) Then
        '
        Else
            TopIndex = i + 1
            Exit For
        End If
        DoEvents
    Next i
    If i <= 0 Then MsgBox "程序错误,结束提取过程", vbExclamation, "警告": Exit Sub
    For i = PoinTer To Len(tmpTxt)
        AscCode = Asc(Mid(tmpTxt, i, 1))
        If AscCode = 46 Or (AscCode >= 48 And AscCode <= 57) Or (AscCode >= 64 And AscCode <= 90) Or AscCode = 95 Or (AscCode >= 97 And AscCode <= 122) Then
        Else
            DownIndex = i - 1
            Exit For
        End If
        DoEvents
    Next i
    If i >= Len(tmpTxt) Then MsgBox "发生未知错误或到文档尾未发现E-mail信息", vbExclamation, "警告": Exit Sub
    GetEmail=GetEmail & Mid(tmpTxt, TopIndex, DownIndex - TopIndex + 1) & vbCrlf
    PoinTer = InStr(DownIndex, tmpTxt, "@")
    DoEvents
Loop
End Function

经检测,这个函数可以将TmpTxt传入的文本中的email准确的提取出来。目前没有进行代码优化,属于初始版本,欢迎大家学习优化!

回复列表 (共6个回复)

沙发

再看看正则表达式吧

板凳

[quote]再看看正则表达式吧[/quote]
正则表达式太复杂

3 楼

[quote]\b[a-zA-Z0-9\.]+@[a-zA-Z0-9\.]+\b[/quote]

[url]http://regexpal.com/[/url]
楼主可以在线测试下,根据你的要求微调下正则表达式

[url]http://www.oicto.com/html/regex.htm[/url]
正则不难,半天时间学习下,可以大大地提高文本处理效率

4 楼

1.把分隔符,;:()<>{}全部转换成空格
2.把双空格换成单空格,(是否要转换多次?)
3.按空格提取子串
4.在子串中找@符号
  4.1 前后 是否 认可 中文 邮箱地址.
  4.2 往后无点号.则舍弃.

5 楼

[quote][quote]\b[a-zA-Z0-9\.]+@[a-zA-Z0-9\.]+\b[/quote]

[url]http://regexpal.com/[/url]
楼主可以在线测试下,根据你的要求微调下正则表达式

[url]http://www.oicto.com/html/regex.htm[/url]
正则不难,半天时间学习下,可以大大地提高文本处理效率[/quote]
好吧,我学习下。

6 楼

这个问题绝对是正则好使

我来回复

您尚未登录,请登录后再回复。点此登录或注册