2009-11-09 99 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