C# 根据正则表达式匹配索引插入字符串

C# 根据正则表达式匹配索引插入字符串,c#,.net,regex,C#,.net,Regex,我正在尝试在每个正则表达式匹配项之前插入新行。目前我得到一个ArgumentOutofRange异常。我意识到索引需要对我插入的所有新行字符进行偏移(总共4个字符) 你们知道怎么解决这个问题吗 谢谢 string origFileContents = File.ReadAllText(path); string cleanFileContents = origFileContents.Replace("\n", "").Replace("\r", ""); Regex regex = new

我正在尝试在每个正则表达式匹配项之前插入新行。目前我得到一个ArgumentOutofRange异常。我意识到索引需要对我插入的所有新行字符进行偏移(总共4个字符)

你们知道怎么解决这个问题吗

谢谢

string origFileContents = File.ReadAllText(path);

string cleanFileContents = origFileContents.Replace("\n", "").Replace("\r", "");

Regex regex = new Regex(@"([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9a-zA-Z]*--)", RegexOptions.Singleline);
MatchCollection matches = regex.Matches(cleanFileContents);

int counter = 0;

foreach (Match match in matches)
{
    cleanFileContents.Insert(match.Index + 4 * counter, Environment.NewLine);
    counter++;
}

我没有遵循
匹配。索引+4*计数器
你知道*是在+之前应用的吗

与Cyborgx37类似-在我开始这篇文章时,它没有发布
ReadAllLines拆分联机提要可能更快

Regex regex = new Regex(@"([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9a-zA-Z]*--)", RegexOptions.Singleline);
StringBuilder sbAll = new StringBuilder();
StringBuilder sbLine = new StringBuilder();
foreach (string line in System.IO.File.ReadAllLines("path"))
{
    sbLine.Append(line);
    MatchCollection matches = regex.Matches(line);

    int counter = 0;

    foreach (Match match in matches)
    {
        sbLine.Insert(match.Index + Environment.NewLine.Length * counter, Environment.NewLine);
        counter++;
    }
    sbAll.Append(line);
    sbLine.Clear();
}

我没有遵循
匹配。索引+4*计数器
你知道*是在+之前应用的吗

与Cyborgx37类似-在我开始这篇文章时,它没有发布
ReadAllLines拆分联机提要可能更快

Regex regex = new Regex(@"([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9a-zA-Z]*--)", RegexOptions.Singleline);
StringBuilder sbAll = new StringBuilder();
StringBuilder sbLine = new StringBuilder();
foreach (string line in System.IO.File.ReadAllLines("path"))
{
    sbLine.Append(line);
    MatchCollection matches = regex.Matches(line);

    int counter = 0;

    foreach (Match match in matches)
    {
        sbLine.Insert(match.Index + Environment.NewLine.Length * counter, Environment.NewLine);
        counter++;
    }
    sbAll.Append(line);
    sbLine.Clear();
}
为什么不

cleanFileContents = regex.Replace(
    cleanFileContents,
    Environment.NewLine + "$0");
也就是说,您的问题可能是Environment.NewLine.Length可能是2,而不是4。编辑:此外,正如Cyborg所指出的,Insert不会在适当的位置修改字符串,而是返回一个新字符串

顺便说一句,如果你想匹配文字括号,你需要避开它们。

为什么不

cleanFileContents = regex.Replace(
    cleanFileContents,
    Environment.NewLine + "$0");
也就是说,您的问题可能是Environment.NewLine.Length可能是2,而不是4。编辑:此外,正如Cyborg所指出的,Insert不会在适当的位置修改字符串,而是返回一个新字符串


顺便说一句,如果您试图匹配文字括号,则需要对其进行转义。

我看到此代码中至少存在这些可识别的问题

  • “\r\n”
    是两个字符,而不是4个。您应该使用
    Environment.NewLine.Length*计数器

  • cleanFileContents.Insert(…)
    返回一个新字符串,它不修改“cleanFileContents”。您需要类似于
    cleanFileContents=cleanFileContents.Insert(…)

  • 建议的编辑:

    string origFileContents = File.ReadAllText(path);
    
    // Changed cleanFileContents to a StringBuilder for performance reasons
    var cleanFileContents = New StringBuilder( origFileContents.Replace("\n", "").Replace("\r", "") );
    
    Regex regex = new Regex(@"([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9a-zA-Z]*--)", RegexOptions.Singleline);
    MatchCollection matches = regex.Matches(cleanFileContents.ToString());
    
    int counter = 0;
    
    foreach (Match match in matches)
    {
        cleanFileContents.Insert(match.Index + Environment.NewLine.Length * counter, Environment.NewLine);
        counter++;
    }
    
    var result = cleanFileContents.ToString()
    

    我在这段代码中至少看到了这些可识别的问题

  • “\r\n”
    是两个字符,而不是4个。您应该使用
    Environment.NewLine.Length*计数器

  • cleanFileContents.Insert(…)
    返回一个新字符串,它不修改“cleanFileContents”。您需要类似于
    cleanFileContents=cleanFileContents.Insert(…)

  • 建议的编辑:

    string origFileContents = File.ReadAllText(path);
    
    // Changed cleanFileContents to a StringBuilder for performance reasons
    var cleanFileContents = New StringBuilder( origFileContents.Replace("\n", "").Replace("\r", "") );
    
    Regex regex = new Regex(@"([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9a-zA-Z]*--)", RegexOptions.Singleline);
    MatchCollection matches = regex.Matches(cleanFileContents.ToString());
    
    int counter = 0;
    
    foreach (Match match in matches)
    {
        cleanFileContents.Insert(match.Index + Environment.NewLine.Length * counter, Environment.NewLine);
        counter++;
    }
    
    var result = cleanFileContents.ToString()
    


    为什么你要用
    计数器
    乘以4?@TimPietzcker-也许他是想补偿字符串变长了?在这种情况下,它应该是
    Environment.NewLine.Length*计数器
    ?Environment.NewLine inserts\r\n,即4个字符。我认为match.index是基于字符串的旧长度的,因为我在每次迭代中插入4个字符,所以我需要对此进行补偿。否?@bjoern-
    “\r\n”
    是两个字符(也许您想的是字节?)仅供参考,该选项没有任何用处。它所做的只是改变点元字符(
    )的行为,并且正则表达式中没有点。为什么要用
    计数器
    乘以4?@TimPietzcker-也许他是在试图补偿字符串变长的问题?在这种情况下,它应该是
    Environment.NewLine.Length*计数器
    ?Environment.NewLine inserts\r\n,即4个字符。我认为match.index是基于字符串的旧长度的,因为我在每次迭代中插入4个字符,所以我需要对此进行补偿。否?@bjoern-
    “\r\n”
    是两个字符(也许您想的是字节?)仅供参考,该选项没有任何用处。它所做的只是改变点元字符(
    )的行为,在正则表达式+1中没有用于识别索引数学问题的点,但还有一个您没有解决的微妙错误-当文本插入“cleanFileContents”字符串时,字符串变长。这意味着匹配索引和字符串将不再同步。它们应该以相反的顺序插入以克服这个问题。@Cyborgx37我没有解决这个微妙的错误,因为我认为插入不是正确的方法。乘法和加法进行得很好,只是乘法器是错误的。您的StringBuilder解决方案与OP试图实现的完全不同,除非输入中只有匹配项。嗯,Environment.newline插入\r\n,即4个字符。我认为match.index是基于字符串的旧长度的,因为我在每次迭代中插入4个字符,所以我需要对此进行补偿。没有?哦。。。乘法是有意的,思维是正确的(虽然大小不正确)。+1用于识别索引数学问题,但还有一个您没有解决的微妙错误-当文本插入“cleanFileContents”字符串时,字符串变长。这意味着匹配索引和字符串将不再同步。它们应该以相反的顺序插入以克服这个问题。@Cyborgx37我没有解决这个微妙的错误,因为我认为插入不是正确的方法。乘法和加法进行得很好,只是乘法器是错误的。您的StringBuilder解决方案与OP试图实现的完全不同,除非输入中只有匹配项。嗯,Environment.newline插入\r\n,即4个字符。我认为match.index是基于字符串的旧长度的,因为我在每次迭代中插入4个字符,所以我需要对此进行补偿。没有?哦。。。乘法是有意的,思维是正确的(尽管大小不正确)。谢谢!我需要使用match.Index+Environment.NewLine.Length*计数器,因为斜杠不会增加长度。我不知道#3、你是对的,菜鸟错了#4匹配索引是匹配的第一个字符的索引。我想在比赛前插入新行。@bjoern-已经大量更新了我的“列表”。一切都很好,除了乘法的大小和
    Insert
    返回一个新字符串。@bjoern-我还建议您使用StringBuilde