blah blah blah is here! blah blah » Close

up1down
link

Hi all,

I'm useless at regex, but for the task I'm currently working on it seems like the correct approach. I have a Windows Service (.NET 2.0) which needs to filter e-mail addresses.
It will receive input in a format like these:

library1@domain.com
library2@domain.dk
library3@subdomain.domain.com
etc..


What i need to do is format them into this:

library1.domain@predefined-domain.dk
library2.domain@predefined-domain.dk
library3.subdomain.domain@predefined-domain.dk


If anyone can point (or throw) me in the right direction I'd appreciate it :)


EDIT: I came up with this:

public static string FormatEmail(string inputAddress, string newDomain)
{
string result = inputAddress.Replace("@", ".");
foreach (string domain in topleveldomains)
{
if (result.LastIndexOf(domain) == result.Length - domain.Length)
{
result = result.Substring(0, result.LastIndexOf(domain));
result += "@" + newDomain;
break;
}
}
return result;
}

last answered 4 months ago

1 answers

link

Well, you could use regex but, if I've understood the pattern correctly, I think you'll find it easier and quicker to use ordinary string parsing here:

using System;

class Test
{
static void Main()
{
string[] addresses =
{
"library1@domain.com",
"library2@domain.dk",
"library3@subdomain.domain.com"
};

foreach(string address in addresses)
{
Console.WriteLine(Reformat(address));
}

Console.ReadKey();
}

static string Reformat(string address)
{
string[] items = address.Split('@');
if (items.Length != 2) return ""; // or whatever
int index = items[1].LastIndexOf('.');
return items[0] + "." + items[1].Substring(0, index) + "@predefined-domain.dk";
}
}

foamy
2135

That's somewhat simpler than the solution I came up with. They both seem to produce the same result though :) Thanks a bunch

Feedback