2

I have this string of proxy addresses, they are separated by an space, however x400 and x500 handles spaces into their addresses. What's the best approach to split it.

e.g.

smtp:[email protected] smtp:[email protected] smtp:[email protected] X400:C=us;A= ;P=mygot;O=Exchange;S=John;G=Gleen; SMTP:[email protected] 

Expected result:

smtp:[email protected]
smtp:[email protected]
smtp:[email protected]
X400:C=us;A= ;P=mygot;O=Exchange;S=John;G=Gleen;
SMTP:[email protected]

thanks,

EDIT,

        string mylist = "smtp:[email protected] smtp:[email protected] smtp:[email protected] X400:C=us;A= ;P=mygot;O=Exchange;S=John;G=Gleen; SMTP:[email protected] X500:/o=Example/ou=USA/cn=Recipients of  /cn=juser smtp:myaddress";

        string[] results = Regex.Split(mylist, @" +(?=\w+:)");
        foreach (string part in results)
        {
            Console.WriteLine(part);
        }

Result

smtp:[email protected]
smtp:[email protected]
smtp:[email protected]
X400:C=us;A= ;P=mygot;O=Exchange;S=John;G=Gleen;
SMTP:[email protected]
X500:/o=Example/ou=USA/cn=Recipients of  /cn=juser
smtp:myaddress
3
  • 1
    Well you could split on SMTP, then add that back in, or it's time to have fun with regular expressions. Commented Dec 11, 2012 at 23:00
  • Get the substring between smtp and x400,x400 and the next smtp. Then split on the individual strings (in fact only one string.. the first substring). Commented Dec 11, 2012 at 23:04
  • OP - do you have control over the input? Commented Dec 11, 2012 at 23:06

4 Answers 4

5

Here is a Regex that should match the spaces before protocols. Try plugging it into Regex.Split like so:

string[] results = Regex.Split(input, @" +(?=\w+:)");
Sign up to request clarification or add additional context in comments.

Comments

1
int index = smtp.indexOf("X400") ;
string[] smtps = smtpString.SubString(0,index).Split(" ") ;
int secondIndex  = smtpString.indexOf("SMTP");
string xfour = smtpString.substring(index,secondIndex);
string lastString = smtpString.indexOf(secondIndex) ;

Should work, if the string format is that way.. and if I didn't screw up the indexes.. although you might want to check if the index isn't -1

Comments

1

Try this:

public static string[] SplitProxy(string text)
        {
            var list = new List<string>();
            var tokens = text.Split(new char[] { ' ' });
            var currentToken = new StringBuilder();

            foreach (var token in tokens)
            {
                if (token.ToLower().Substring(0, 4) == "smtp")
                {
                    if (currentToken.Length > 0)
                    {
                        list.Add(currentToken.ToString());
                        currentToken.Clear();
                    }

                    list.Add(token);
                }
                else
                {
                    currentToken.Append(token);
                }
            }

            if (currentToken.Length > 0)
                        list.Add(currentToken.ToString());

            return list.ToArray();
        }

It splits the string by spaces into tokens then goes through them one by one. If the token starts with smtp it is added to the result array. If not, that token is concatted with the following tokens to create one entry than is added to the result array. Should work with anything that has spaces and doesn't start with smtp.

Comments

-1

I reckon the following line should do the work

var addrlist = variable.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);

1 Comment

I don't think that will work because there's a space after A=.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.