【问题标题】:Replacing missing "newline" in pre-processed text替换预处理文本中缺少的“换行符”
【发布时间】:2015-12-01 01:08:26
【问题描述】:

我继承了一个电子邮件数据库表,其中保存的正文已被处理以删除变音符号,但此过程也删除了 Environment.Newline 字符。我可以编写一个正则表达式来识别这种模式([.!?.!?]\ {0}\w),因为在典型的句尾标记(例如:.!?)和开头之间没有空格下一句,但我看不到如何在两个字符之间插入换行符。

E:g: "这是第一段的结尾。这是第二段的开头。

我想插入一个新行(在这种情况下,在“h.A”之间)和出现这种类型的模式的任何地方。任何帮助将不胜感激(我使用 C# .NET 4.5)- 我已经在 RegExBuddy 上花费了数小时,但看不到如何操作。请原谅我的无知。

【问题讨论】:

    标签: c# regex


    【解决方案1】:

    首先,我会考虑通过推送来获取原始消息,而不是采取这些措施,因为结果并不完美。

    您可以使用正则表达式[\.\!\?]\b,其定义为标点符号后跟单词的开头。

    示例代码:

    static void Main(string[] args)
    {
        Console.WriteLine(RestoreNewlines("This is the end of the first paragraph.And this is the start of the second. This is the start of the third."));
        Console.WriteLine(RestoreNewlines("Example of a case.txt where it fails."));
    }
    
    private static readonly Regex PunctuationWithoutFollowingWhitespaceRegex = new Regex(@"[\.\!\?]\b");
    
    static string RestoreNewlines(string input)
    {
        return PunctuationWithoutFollowingWhitespaceRegex.Replace(input, match => match.Value + Environment.NewLine);
    }
    

    输出:

    This is the end of the first paragraph.
    And this is the start of the second. This is the start of the third.
    Example of a case.
    txt where it fails.
    

    【讨论】:

    • Ps,如果电子邮件可用,我会恢复它们:它们已被他们使用的工具从服务器上弹出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    相关资源
    最近更新 更多