2015-09-04 31 views
1

我已經繼承了一個電子郵件數據庫表,其中已經處理了保存的正文文本以刪除變音符號,但此過程還取出了Environment.Newline字符。我可以編寫一個正則表達式來識別此模式([。!?。??] \ {0} \ w),因爲在句子標記的典型結尾(例如:。!?)之間不存在空格,下一句,但我看不到如何在兩個字符之間插入換行符。在預處理文本中替換丟失的「換行符」

E:G:「這是第一paragraph.And此的端部是所述第二的開始

我要插入一個新行(在之間這種情況下,‘HA’)和地方這類型的模式發生任何幫助將不勝感激(我使用C#.NET 4.5) - 我已經花了幾個小時的RegExBuddy,看不到如何做到這一點,請原諒我的無知

回答

0

首先, d考慮推送獲取原始消息而不是訴諸於這些措施,因爲結果不會很完美。

您可以使用正則表達式[\.\!\?]\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. 
+0

PS,我會恢復的郵件,如果他們被查閱:他們被彈出通過他們使用的工具的服務器。 –