将一个字符串拆分为另一个字符串,除了

Split a string by another string except by

我想使用分隔符 : 用 Regex 拆分这一行,除非该段包含 ?:

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

我试过

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

但是所有元素的最后一个字符都被截断了,

我正在等待这个结果:

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

https://dotnetfiddle.net/PeVuMM

如果我理解了你的问题,下面的示例将给出预期的输出:)

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);
        }
    }
}