【问题标题】:Replace text which contain line breaks without dropping them替换包含换行符的文本而不删除它们
【发布时间】:2019-11-02 18:50:33
【问题描述】:

我们有一个这样的文本..

This is text
                i want
                to keep
                    but
                Replace this sentence
                because i dont like it.

现在我想替换这句话Replace this sentence because i dont like it.

当然会这样

text = text.Replace(@"Replace this sentence because i dont like it.", "");

无法解决我的问题。 我不能删除换行符并用一行替换它们。

我的输出应该是

This is text
                i want
                to keep
                    but

请记住,我不喜欢的句子有很多变体和换行符。

I.E 它可能会像

Replace this 
sentence
                    because i dont like it.

Some text before. Replace this 
    sentence
                        because i dont like it.

【问题讨论】:

    标签: c#


    【解决方案1】:

    您可以使用正则表达式来查找任何类型的空格。这包括常规空格,但也包括回车和换行以及制表符或半空格等。

    string input = @"This is text
        i want
        to keep
            but
        Replace this sentence
        because i dont like it.";
    
    string dontLike = @"Replace this sentence because i dont like it.";
    string pattern = Regex.Escape(dontLike).Replace(@"\ ", @"\s+");
    
    Console.WriteLine("Pattern:");
    Console.WriteLine(pattern);
    
    string clean = Regex.Replace(input, pattern, "");
    
    Console.WriteLine();
    Console.WriteLine("Result:");
    Console.WriteLine(clean);
    Console.ReadKey();
    

    输出:

    Pattern:
    Replace\s+this\s+sentence\s+because\s+i\s+dont\s+like\s+it\.
    
    Result:
    This is text
                    i want
                    to keep
                        but
    

    Regex.Escape 转义任何在正则表达式中具有特殊含义的字符。例如,句点"." 表示“任意次数的重复”。它还将空格" " 替换为@"\ "。我们反过来将搜索模式中的@"\ " 替换为@"\s+"。正则表达式中的\s+ 表示“一个或多个空格”。

    【讨论】:

      【解决方案2】:

      使用正则表达式来匹配“任何空格”,而不仅仅是搜索字符串中的空格。大概

      【讨论】:

        【解决方案3】:

        或者,使用 LINQ 来完成:

        var text = "Drones " + Environment.NewLine + "are great to fly, " + Environment.NewLine + "yes, very fun!";
        var textToReplace = "Drones are great".Split(" ").ToList();
        textToReplace.ForEach(f => text = text.Replace(f, ""));
        

        输出:

        to fly, 
        
        yes, very fun!
        

        无论您选择哪种方法,您都将处理额外的换行符、过多的空格和其他格式问题...祝您好运!

        【讨论】:

          【解决方案4】:

          如果字符串的输出格式在这里是可选的,你可以使用这样的东西:

          using System;
          using System.Text.RegularExpressions;
          
          public class Program
          {
              public static void Main()
              {
                  string textToReplace = @"Replace this sentence because i dont like it.";
                  string text = @"This is text
                          i want
                          to keep
                              but
                          Replace this sentence
                          because i dont like it.";
          
                  text = Regex.Replace(text, @"\s+", " ", RegexOptions.Multiline);
                  text = text.Replace(textToReplace, string.Empty);
                  Console.WriteLine(text);
              }
          }
          

          输出:

          "This is text i want to keep but"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-11-04
            • 2023-03-10
            • 2016-04-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多