Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 拆分多个字符,同时将字符保留在结果中_C#_Regex_Split - Fatal编程技术网

C# 拆分多个字符,同时将字符保留在结果中

C# 拆分多个字符,同时将字符保留在结果中,c#,regex,split,C#,Regex,Split,我想在拆分结果中保留这些字符的同时,拆分'0'和'1'。我怎样才能在C#中做到这一点 e、 g 我试过了 Regex.Split(number, @"(?=[01])"); 但是我得到的结果是”,012345 除了两次拆分之间的“”之外,以下操作似乎有效 Regex.Split(number, @"(0|1)"); 您可以使用LINQ使用您在文章中提到的正则表达式模式简单地排除空元素: var results = Regex.Split(number, @"(0|1)")

我想在拆分结果中保留这些字符的同时,拆分
'0'
'1'
。我怎样才能在C#中做到这一点

e、 g

我试过了

Regex.Split(number, @"(?=[01])");
但是我得到的结果是
”,012345

除了两次拆分之间的
”之外,以下操作似乎有效

Regex.Split(number, @"(0|1)");

您可以使用LINQ使用您在文章中提到的正则表达式模式简单地排除空元素:

var results = Regex.Split(number, @"(0|1)")
                   .Where(p => !String.IsNullOrEmpty(p));
这也可以奏效。我希望看到一个更优雅的方法,我觉得这是你所追求的,但它完成了工作

List<string> results = new List<string>();
int curInd = 0;

var matchInfo = Regex.Matches(number, "(0|1)");
foreach (Match m in matchInfo)
{
    //Currently at the start of a match, add the match sequence to the result list.
    if (curInd == m.Index)
    {
        results.Add(number.Substring(m.Index, m.Length));
        curInd += m.Length;  
    }
    else  //add the substring up to the match point and then add the match itself
    {
        results.Add(number.Substring(curInd, m.Index - curInd));
        results.Add(number.Substring(m.Index, m.Length));
        curInd = m.Index + m.Length;
    }
}
//add any remaining text after the last match
if (curInd < number.Length)
{
    results.Add(number.Substring(curInd));
}
列表结果=新列表();
int-curInd=0;
var matchInfo=Regex.Matches(数字,“(0 | 1)”);
foreach(在matchInfo中匹配m)
{
//当前,在匹配开始时,将匹配序列添加到结果列表中。
if(curInd==m.Index)
{
结果:添加(数字、子字符串(m.Index、m.Length));
curInd+=米长;
}
else//将子字符串添加到匹配点,然后添加匹配本身
{
结果.添加(数字.子字符串(curInd,m.索引-curInd));
结果:添加(数字、子字符串(m.Index、m.Length));
curInd=米指数+米长度;
}
}
//在最后一次匹配后添加任何剩余文本
if(居里数<数字长度)
{
结果。添加(数字。子字符串(curInd));
}

请不要在标题前加上“C”之类的前缀。这就是标签的用途。根据上面现有的答案,可能有重复的:
Regex.Split(value,@)(@Philip:你能把它作为答案发布吗?@JamesCotter:当我将它粘贴到linqpad:Regex.Split(“012345”),对我来说很好?
List<string> results = new List<string>();
int curInd = 0;

var matchInfo = Regex.Matches(number, "(0|1)");
foreach (Match m in matchInfo)
{
    //Currently at the start of a match, add the match sequence to the result list.
    if (curInd == m.Index)
    {
        results.Add(number.Substring(m.Index, m.Length));
        curInd += m.Length;  
    }
    else  //add the substring up to the match point and then add the match itself
    {
        results.Add(number.Substring(curInd, m.Index - curInd));
        results.Add(number.Substring(m.Index, m.Length));
        curInd = m.Index + m.Length;
    }
}
//add any remaining text after the last match
if (curInd < number.Length)
{
    results.Add(number.Substring(curInd));
}