从vb.net的字符串解析电子邮件地址

问题描述:

希望有人能帮助我弄清楚这一点。从vb.net的字符串解析电子邮件地址

我已经获得了从交换机导出的分发列表的导出负载。

的出口是这样的...... enter image description here

我正在写一个小程序来解析这些吐出可倾倒直入交换外壳重新创建列表的脚本。

到目前为止,我已经阅读了输出,并逐行循环阅读。

我想要做的是在行中找到@符号的位置,然后抓住它之前的所有内容,直到遇到空间,并且之后的所有内容直到遇到空间。

我已经在excel中完成了一百万次这样的事情,但是从来没有在vb.net中做过,我不知道如何实现它。

对不起,如果这看起来像一个愚蠢的问题;他们只是容易,如果你知道答案:)

到目前为止,我有这个(感谢Pikoh!)...

If System.IO.File.Exists(strFileName) = True Then 

    Dim objReader As New System.IO.StreamReader(strFileName) 

    Do While objReader.Peek() <> -1 

     TextLine = TextLine & objReader.ReadLine() & vbNewLine 

    Loop 

    output.Text = TextLine 


    For Each line As String In output.Text.Split(vbLf) 
     Try 
      Dim testStrings As String() = New String() {line} 
      Dim stringSeparators() As String = {" ", "\t"} 
      Dim email1 = testStrings(0).Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)(1) 
      Dim email2 = testStrings(0).Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)(1) 


      MessageBox.Show(email1 & "|" & email2) 
     Catch ex As Exception 
      MessageBox.Show("Nothing :(") 
     End Try 


    Next 

End If 
+1

['IndexOf'](https://msdn.microsoft.com/en-us/library/k8b1470s(V = vs.110)的.aspx)? – Pikoh

+0

谢谢@ Pikoh ...我可以用它来找到'@'的位置......然后我如何找到相对于'@'的左右空格? – John

+1

使用'Split'查看我的答案。 – Pikoh

对于简单的情况就是这样,我最简单的方法认为只是使用Split。由于我不知道2列之间有什么,所以我要定义2个分隔符:空格和制表符:

Dim testStrings As String() = New String() {"test  [email protected]", "test2" & vbTab & "[email protected]"} 
Dim stringSeparators() As String = {" ", vbTab} 
Dim email1 = testStrings(0).Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries).Last() 
Dim email2 = testStrings(0).Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries).Last() 

您仍然可以以相同的方式执行此操作。事实上,你的VBA代码可能在VB.NET中有效。

随着测试文件:

Name      PrimarySmtpAddress 
----      ------------------ 
_Sys.User     [email protected]  
Joe Bloggs    [email protected] 
Bill Gates    [email protected] 
J. Someone Else    [email protected] 
Matt "[email protected]" Pain   [email protected] 

和代码

Option Infer On 
Option Strict On 

Imports System.IO 

Module Module1 

    Function ExtractEmailAddress(s As String) As String 
     Dim atPos = s.LastIndexOf("@"c) 
     If atPos < 0 Then 
      Return String.Empty 
     End If 

     Dim firstPos = atPos + 1 
     Dim lastPos = atPos - 1 

     Do 
      firstPos -= 1 
     Loop Until firstPos = 0 OrElse String.IsNullOrWhiteSpace(s.Chars(firstPos)) 

     Do 
      lastPos += 1 
     Loop Until lastPos = s.Length OrElse String.IsNullOrWhiteSpace(s.Chars(lastPos)) 

     Return s.Substring(firstPos + 1, lastPos - firstPos - 1) 

    End Function 

    Sub Main() 
     Using sr As New StreamReader("C:\temp\ExampleOutput.txt") 
      While Not sr.EndOfStream 
       Dim line = sr.ReadLine() 
       Dim email = ExtractEmailAddress(line) 
       If email.Length > 0 Then 
        Console.WriteLine(email) 
       End If 

      End While 
     End Using 

     Console.ReadLine() 

    End Sub 

End Module 

输出为:

[email protected]
joe.bloggs @ domain.com
[email protected]
[email protected]
[email protected]