Search code examples
c#regexsplitexcept

Split a string by another string except by


I want to split with Regex this line using the separator : except when the segment contains ?:

string input = "AV Rocket 456:Contact?:Jane or Tarza:URL?:http?://www.jane.com:Delivered 18?:15:Product Description";

I tried with

string pattern = @"[^\?:]\:";

But the last char at all elements are cutted,

I am waiting for this result:

'AV Rocket 456'
'CONTACT?:JANE OR TARZAN'
'URL?:http?://www.jane.com'
'Time Delivered 18?:15'
'Product Description'

https://dotnetfiddle.net/PeVuMM


Solution

  • If I have understood your question, the below sample will give the intended output :)

    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using Xunit;
    
    namespace XUnitTestProject1
    {
        public class UnitTest1
        {
            [Fact]
            public void TestPatternSplit()
            {
    
                var input = @"AV Rocket 456:Contact?:Jane or Tarzan:URL?:http?://www.jane.com:Time Delivered 18?:15:Product Description";
    
                var output = PatternSplit(input);
    
                var expected = new[]{"AV Rocket 456", "Contact?:Jane or Tarzan", "URL?:http?://www.jane.com","Time Delivered 18?:15","Product Description"};
    
                Assert.Equal(expected, output);
    
            }
    
            private static IEnumerable<string> PatternSplit(string input)
            {
                const string  pattern = @"(?<!\?):";
                return Regex.Split(input, pattern);
            }
        }
    }