2009-11-09 98 views
2

我有一個要求。在C中替換字符#

我有一個可以包含任何字符的文本。

a)我只能保留字母數字字符 b)如果找到帶有前綴或後綴空格的單詞「The」,則需要將其刪除。

例如

CASE 1: 

Input: The Company Pvt Ltd. 

Output: Company Pvt Ltd 

But 

    Input: TheCompany Pvt Ltd. 

    Output: TheCompany Pvt Ltd 

because there is no space between The & Company words. 

CASE 2: 

Similarly, Input: Company Pvt Ltd. The 

    Output: Company Pvt Ltd 

But Input: Company Pvt Ltd.The 

    Output: Company Pvt Ltd 

Case 3: 

Input: [email protected] Pvt; Ltd. 

Output: Company234 Pvt Ltd 

No , or . or any other special characters. 

我基本上因此,在節能,我不能做任何事情的時候,數據設定一些變量像

_company.ShortName = _company.CompanyName.ToUpper(); 

。只有當我從數據庫中獲取數據時,我才需要應用此過濾器。數據進來_company.CompanyName

我必須應用該過濾器。

到目前爲止,我已經做了提前

public string ReplaceCharacters(string words) 
{ 
    words = words.Replace(",", " "); 
    words = words.Replace(";", " "); 
    words = words.Replace(".", " "); 
    words = words.Replace("THE ", " "); 
    words = words.Replace(" THE", " "); 
    return words; 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    MessageBox.Show(ReplaceCharacters(textBox1.Text.ToUpper())); 
} 

感謝。我正在使用C#

+0

在案例1,2中,結果上有點,但是在3上刪除了它們。 – Kobi 2009-11-09 05:47:43

+0

它失敗,在這種情況下theasdasdathe的的的....蘋果,,,,輸出是:theasdasdaapple預期輸出:theasdasdatheapple – 2009-11-09 06:28:36

+0

了Kobi這是一個錯誤,打字時..這將編輯..不應該有任何特殊的字符。感謝您的通知.. – 2009-11-09 07:32:33

回答

10

這是一個基本的正則表達式,它與您提供的案例相匹配。正如Kobi所說,由於你提供的案例不一致,所以我已經從前四個測試中抽出了時間。如果您同時需要,請添加評論。

這可以處理您需要的所有情況,但邊緣案例的迅速擴散使我認爲,也許您應該重新考慮最初的問題?

[TestMethod] 
    public void RegexTest() 
    { 
     Assert.AreEqual("Company Pvt Ltd", RegexMethod("The Company Pvt Ltd")); 
     Assert.AreEqual("TheCompany Pvt Ltd", RegexMethod("TheCompany Pvt Ltd")); 
     Assert.AreEqual("Company Pvt Ltd", RegexMethod("Company Pvt Ltd. The")); 
     Assert.AreEqual("Company Pvt LtdThe", RegexMethod("Company Pvt Ltd.The")); 
     Assert.AreEqual("Company234 Pvt Ltd", RegexMethod("[email protected] Pvt; Ltd.")); 
     // Two new tests for new requirements 
     Assert.AreEqual("CompanyThe Ltd", RegexMethod("CompanyThe Ltd.")); 
     Assert.AreEqual("theasdasdatheapple", RegexMethod("the theasdasdathe the the the ....apple,,,, the")); 
     // And the case where you have THETHE at the start 
     Assert.AreEqual("CCC", RegexMethod("THETHE CCC")); 
    } 

    public string RegexMethod(string input) 
    { 
     // Old method before new requirement   
     //return Regex.Replace(input, @"The | The|[^A-Z0-9\s]", string.Empty, RegexOptions.IgnoreCase); 
     // New method that anchors the first the   
     //return Regex.Replace(input, @"^The | The|[^A-Z0-9\s]", string.Empty, RegexOptions.IgnoreCase);    
     // And a third method that does look behind and ahead for the last test 
     return Regex.Replace(input, @"^(The)+\s|\s(?<![A-Z0-9])[\s]*The[\s]*(?![A-Z0-9])| The$|[^A-Z0-9\s]", string.Empty, RegexOptions.IgnoreCase); 
    } 

我還在我的示例中添加了一個測試方法,該方法執行包含正則表達式的RegexMethod。要在你的代碼中使用它,你只需要第二種方法。

+0

在看什麼我提供 - 它符合你要求什麼,但也有可能的邊緣案件數十種。比如當「The」出現在公司名稱中間時 - 是否應該刪除?有很多方法可以滿足大多數需求,但您首先需要明確這些要求。 – 2009-11-09 06:01:46

+0

對於測試用例,我假設在實際方法之前寫入+1。 – 2009-11-09 06:06:52

+0

不是很暴躁,但我認爲這是比一堆* .Replace()調用更好的一段代碼。另一方面,通過首先展示測試用例,對那些不習慣這種方法的人來說,答案變得不那麼容易理解和接近。 – 2009-11-09 06:13:00

2
string company = "Company; PvtThe Ltd.The . The the.the"; 
company = Regex.Replace(company, @"\bthe\b", "", RegexOptions.IgnoreCase); 
company = Regex.Replace(company, @"[^\w ]", ""); 
company = Regex.Replace(company, @"\s+", " "); 
company = company.Trim(); 
// company == "Company PvtThe Ltd" 

這些是步驟。 1和2可以合併,但這更清楚。

  1. 刪除 「的」 整體字(也適用於 「.the」)。
  2. 刪除任何不是字母或空格的東西。
  3. 刪除所有相鄰的空格。
  4. 從邊緣移除空格。
+0

科比,這是一個錯誤,而輸入..它會編輯..不應該有任何特殊字符。感謝您通知。 – 2009-11-09 07:33:25