3

This is an extension to this SO question. This question considers two different enclosing characters, in contrast to the original question.

I would like to split by (white)spaces of any number but ignore everything between <> AND "". So this string:

string Line = "1  2  <1  2> \"hello world\"   3";

Should result in this:

1, 2, <1 2>, \"hello world\", 3

6
  • If you're ignoring anything between <> and "" shouldn't the result be 1, 2, <1 2>, \"hello world\", 3 (note the 2 whitespaces between <1 and 2>) Commented Dec 14, 2012 at 14:07
  • The number of whitespaces should not matter and nothing should be split if it is enclosed by "" or <>. Commented Dec 14, 2012 at 14:11
  • 1
    @dan1111. It is definitely not a duplicate! Commented Dec 14, 2012 at 14:12
  • @csetzkorn how the heck is that not a duplicate? Because they used square brackets instead of quotes? Commented Dec 14, 2012 at 14:20
  • Yes, the 'answer' which you may refer to uses [. My original question asks explicitly about ONE exception rather than 2 as in THIS question. Commented Dec 14, 2012 at 14:26

2 Answers 2

3

Instead of Split, I'll use Matches

string Line = "1  2  <1  2> \"hello world\"   3";
var parts = Regex.Matches(Line, @"[<\""]{1}[\w \d]+?[>\""]{1}|[\w\d]+")
                 .Cast<Match>()
                 .Select(m=>m.Value)
                 .ToArray();

PS: This would also match "abc def>. But I ignored it to make the regex shorter

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, however it does not seem to work for "" (e.g. "" 1 2 -> "",1,2)
0

This is what I came up with so far:

public static string[] GetSplitStrings(string input)
{
    IList<string> splitStrings = new List<string>();
    var counter = 0;

    var sb = new StringBuilder();
    var inLessGreater = false; // sometimes <> can contain "
    foreach (var character in input)
    {
    if (character.Equals('<'))
    {
        inLessGreater = true;
        counter++;
    }

    if (character.Equals('>'))
    {
        inLessGreater = false;
        counter++;
    }

    if (character.Equals('"') && !inLessGreater)
    {
        counter++;
    }

    if ((character.Equals(' ') && counter == 0) || (counter == 2))
    {
        if (sb.ToString().Equals("") == false)
        {
        if (character.Equals('"') || character.Equals('>'))
        {
            sb.Append(character);
        }
        splitStrings.Add(sb.ToString());
        }
        sb.Clear();
        counter = 0;
    }
    else
    {
        sb.Append(character);
    }
    }

    return splitStrings.ToArray();
}

Would prefer a neat regex solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.