使用正则表达式拆分C#中字符串中的字符串

使用正则表达式拆分C#中字符串中的字符串,c#,regex,C#,Regex,我有一个类似于 SOI1;2.3.4.5.6.7; SOI8;9; 10; 11; 12; EOI13EOI;SOI14;15; 16; 17; 18; EOI 在这里,我必须从SOI开始拆分字符串;到EOI 输出应该是 [0]-1;2.3.4.5.6.7.13; [1] - 8; 9; 10; 11; 12; [2] - 14; 15; 16; 17; 18; 我已尝试使用以下代码进行拆分 string regexexpr = "(?s)(?<=SOI;).+?(?=EOI;)";//

我有一个类似于
SOI1;2.3.4.5.6.7; <强>SOI8;9; 10; 11; 12; <强>EOI13<强>EOI;SOI14;15; 16; 17; 18; <强>EOI

在这里,我必须从SOI开始拆分字符串;到EOI
输出应该是

[0]-1;2.3.4.5.6.7.13;
[1] - 8; 9; 10; 11; 12;
[2] - 14; 15; 16; 17; 18;

我已尝试使用以下代码进行拆分

string regexexpr = "(?s)(?<=SOI;).+?(?=EOI;)";//@"SOI;(.*?)EOI;";
string sText = "SOI; 1; 2; 3; 4; 5;  6; 7;SOI; 8; 9; 10; 11; 12; EOI; 13; EOI; SOI; 14; 15; 16; 17; 18; EOI;";
MatchCollection matches = Regex.Matches(sText, @regexexpr);
var sample = matches.Cast<Match>().Select(m => m.Value);

string regexexpr=“(?s)(?我想我应该按程序来做,而不是使用regexp

编辑:下面的解决方案有一个错误,第一个和第三个列表将是相同的。我离开它,因为它可能仍然是一个正确方向的提示

1) 将值设置为零。
2) 读取字符串中的下一个标记。
3) 如果令牌是SOI,则将1添加到值
4) 如果令牌是EOI,则从值中删除1
5) 如果令牌是一个数字,请根据值将其添加到不同的数组(或列表)。
6) 转到2

专用静态列表GetList(字符串文本)
{
字符串[]输出;
列表输入=新列表();
input=sText.Split(新字符串[]{“},StringSplitOptions.RemoveEmptyEntries.ToList();
int count=input.count(x=>x==“SOI;”);
输出=新字符串[计数];//将输出数组设置为字符串中的列表数
int current=-1;//以-1开头,因此第一个SOI将其设置为0
int max=-1;
foreach(输入中的var文本)
{
如果(文本==“SOI;”//设置当前值和最大值
{
电流++;
max++;
}
else if(文本==“EOI;”)
{
当前--;
if(current==-1)//如果u达到-1,则表示u不在任何列表中,因此将current设置为max,以便如果u将获得“SOI”,则u将获得正确的数字
{
电流=最大值;
}
}
其他的
{
输出[当前]+=文本;
}
}
返回output.ToList();
}
}

您是在问如何提取嵌套的SOI/EOI结构吗?如果您可以使用正则表达式来提取嵌套的SOI/EOI结构,我会感到惊讶。编写代码来处理值列表会更容易。您希望如何
[0]-1;2;3;4;5;6;7;13;
作为输出?当
8;9;10;11;12;
进入下一个匹配时,13从何而来?
    private static List<string> GetLists(string sText)
    {
        string[] output;
        List<string> input = new List<string>();
        input = sText.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries).ToList();
        int count = input.Count(x => x == "SOI;");
        output = new string[count]; // set output array to number of lists in string
        int current = -1;  // start with -1 so first SOI will set it on 0
        int max = -1;
        foreach (var text in input)
        {
            if (text == "SOI;") // set current and max
            {
                current++;
                max++;
            }
            else if (text == "EOI;")
            {
                current--;
                if (current == -1)  // if u reached -1 it means u are out of any list so set current on max so if u will get "SOI" u will get proper number
                {
                    current = max;
                }
            }
            else
            {
                output[current] += text;
            }
        }

        return output.ToList();
    }
}