How can I split this string into an array?

My line looks like this:

smtp:jblack@test.com;SMTP:jb@test.com;X400:C=US;A= ;P=Test;O=Exchange;S=Jack;G=Black;

I need to return:

smtp:jblack@test.com
SMTP:jb@test.com
X400:C=US;A= ;P=Test;O=Exchange;S=Jack;G=Black;

The problem is that half-columns share addresses, as well as part of the X400 address. Can anyone suggest what is the best way to share this?

PS I must mention that the order is different in that it can be:

X400:C=US;A= ;P=Test;O=Exchange;S=Jack;G=Black;;smtp:jblack@test.com;SMTP:jb@test.com

There can be more than 3 addresses, 4, 5 .. 10, etc., including the X500 address, however they all start with SMTP: SMTP: X400 or X500.

+5
source share
12 answers

EDIT: with updated information, this answer will certainly not do the trick, but it is still potentially useful, so I will leave it here.

You will always have three parts, and you just want to break the first two half-columns?

, Split, :

string[] bits = text.Split(new char[]{';'}, 3);
+9

(smtp|SMTP|X400|X500):((?!smtp:|SMTP:|X400:|X500:).)*;?

.*?:((?![^:;]*:).)*;?

, , . . , . ().

, ':', . , , .

, .

, , , . , , , .

+4

string[] items = System.Text.RegularExpressions.Split(text, ";(?=\w+:)");

EDIT: .

string[] items = System.Text.RegularExpressions.Split(text, ";(?=[^;:]+:)");
+3

http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx , , .

string.split(new char[]{';'}, 3);
+1

.... , :)

- , ; =)

static void Main(string[] args)
{
    string fneh = "X400:C=US400;A= ;P=Test;O=Exchange;S=Jack;G=Black;x400:C=US400l;A= l;P=Testl;O=Exchangel;S=Jackl;G=Blackl;smtp:jblack@test.com;X500:C=US500;A= ;P=Test;O=Exchange;S=Jack;G=Black;SMTP:jb@test.com;";

    string[] parts = fneh.Split(new char[] { ';' });

    List<string> addresses = new List<string>();
    StringBuilder address = new StringBuilder();
    foreach (string part in parts)
    {
        if (part.Contains(":"))
        {
            if (address.Length > 0)
            {
                addresses.Add(semiColonCorrection(address.ToString()));
            }
            address = new StringBuilder();
            address.Append(part);
        }
        else
        {
            address.AppendFormat(";{0}", part);
        }
    }
    addresses.Add(semiColonCorrection(address.ToString()));

    foreach (string emailAddress in addresses)
    {
        Console.WriteLine(emailAddress);
    }
    Console.ReadKey();
}
private static string semiColonCorrection(string address)
{
    if ((address.StartsWith("x", StringComparison.InvariantCultureIgnoreCase)) && (!address.EndsWith(";")))
    {
        return string.Format("{0};", address);
    }
    else
    {
        return address;
    }
}
+1

, , , .

        string input1 = "smtp:jblack@test.com;SMTP:jb@test.com;X400:C=US;A= ;P=Test;O=Exchange;S=Jack;G=Black;";
        string input2 = "X400:C=US;A= ;P=Test;O=Exchange;S=Jack;G=Black;;smtp:jblack@test.com;SMTP:jb@test.com";
        Regex splitEmailRegex = new Regex(@"(?<key>\w+?):(?<value>.*?)(\w+:|$)");

        List<string> sets = new List<string>();

        while (input2.Length > 0)
        {
            Match m1 = splitEmailRegex.Matches(input2)[0];
            string s1 = m1.Groups["key"].Value + ":" + m1.Groups["value"].Value;
            sets.Add(s1);
            input2 = input2.Substring(s1.Length);
        }

        foreach (var set in sets)
        {
            Console.WriteLine(set);
        }

        Console.ReadLine();

, Regex: . , , .

+1

, .

string[] bits = text.Split(':');
List<string> values = new List<string>();
for (int i = 1; i < bits.Length; i++)
{
    string value = bits[i].Contains(';') ? bits[i].Substring(0, bits[i].LastIndexOf(';') + 1) : bits[i];
    string key = bits[i - 1].Contains(';') ? bits[i - 1].Substring(bits[i - 1].LastIndexOf(';') + 1) : bits[i - 1];
    values.Add(String.Concat(key, ":", value));
}

, .

+1

. , , .

X400:(?<X400>.*?)(?:smtp|SMTP|$)
smtp:(?<smtp>.*?)(?:;+|$)
SMTP:(?<SMTP>.*?)(?:;+|$)

, . , ,

0

. ;)

string src = "smtp:jblack@test.com;SMTP:jb@test.com;X400:C=US;A= ;P=Test;O=Exchange;S=Jack;G=Black;";

Regex r = new Regex(@"
   (?:^|;)smtp:(?<smtp>([^;]*(?=;|$)))|
   (?:^|;)x400:(?<X400>.*?)(?=;x400|;x500|;smtp|$)|
   (?:^|;)x500:(?<X500>.*?)(?=;x400|;x500|;smtp|$)",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

foreach (Match m in r.Matches(src))
{
    if (m.Groups["smtp"].Captures.Count != 0)
        Console.WriteLine("smtp: {0}", m.Groups["smtp"]);
    else if (m.Groups["X400"].Captures.Count != 0)
        Console.WriteLine("X400: {0}", m.Groups["X400"]);
    else if (m.Groups["X500"].Captures.Count != 0)
        Console.WriteLine("X500: {0}", m.Groups["X500"]);   
}

smtp, x400 x500 . , . smtp, x400 x500 .

0

!

    string input =
        "smtp:jblack@test.com;SMTP:jb@test.com;X400:C=US;A= ;P=Test;O=Exchange;S=Jack;G=Black;";
    string[] parts = input.Split(';');
    List<string> output = new List<string>();
    foreach(string part in parts)
    {
        if (part.Contains(":"))
        {
            output.Add(part + ";");
        }
        else if (part.Length > 0)
        {
            output[output.Count - 1] += part + ";";
        }
    }
    foreach(string s in output)
    {
        Console.WriteLine(s);
    }
0

(;) , , (:) .

string input = "X400:C=US;A= ;P=Test;O=Exchange;S=Jack;G="
  +"Black;;smtp:jblack@test.com;SMTP:jb@test.com";

string[] rawSplit = input.Split(';');

List<string> result = new List<string>();
  //now the fun begins
string buffer = string.Empty;
foreach (string s in rawSplit)
{
  if (buffer == string.Empty)
  {
    buffer = s;
  }
  else if (s.Contains(':'))
  {   
    result.Add(buffer);
    buffer = s;
  }
  else
  {
    buffer += ";" + s;
  }
}
result.Add(buffer);

foreach (string s in result)
  Console.WriteLine(s);
-1

.

string[] bits = text.Replace(";smtp", "|smtp").Replace(";SMTP", "|SMTP").Replace(";X400", "|X400").Split(new char[] { '|' });

[0], [1] [2] .

-1

All Articles