How to find emails in a text file!!!



  • wow - really?

    Do some research on Regular Expressions:
    var path = "C:\text.txt";
    var file = File.ReadAllText(path);

    var emails =
    RegexOptions.Singleline, RegexOptions.IgnoreCase);

    so like other than actually opening the file, this will get every e-mail for you in a single line of code. No splitting, reading line by line, finding text markers, having to assume a specific formatting, and whatever else.

    Now if you wanted to count duplicates you should turn to .net generics...
    var counter = new Dictionary()

    foreach (var email in emails.Cast())
    if (!counter.ContainsKey(email.Value))
    counter.Add(email.Value, 0);

    bam! another 5 lines of code (4 if you make the if statement 1 line)

    regular expressions are amazing for parsing tasks like this. Since this is using pattern matching it actually won't matter how the file is formatted, it will simply go through and pick out everything matching what you have defined as an e-mail address.

    Now if you haven't seen regular expressions before that will just look like garbledegoop up there, but after a little bit of looking them up online you'll find they are very easy to work with.

    as a disclaimer I am assuming the standard for e-mail address naming only includes alphabetic and numeric characters along with '_' and '.'

    so whatever the real standard is, those characters should be included also

    and I'm sorry for using C# in a vb forum - the libraries are all the same so adaptation shouldn't be very difficult.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!