Character 替换C中的字符#
我有一个要求 我有一个可以包含任何字符的文本 a)我必须只保留字母数字字符 b) 如果发现单词“the”的前缀或后缀为空格,则需要将其删除。 e、 g 我基本上是将数据设置为一些变量,比如Character 替换C中的字符#,character,replace,Character,Replace,我有一个要求 我有一个可以包含任何字符的文本 a)我必须只保留字母数字字符 b) 如果发现单词“the”的前缀或后缀为空格,则需要将其删除。 e、 g 我基本上是将数据设置为一些变量,比如 _company.ShortName = _company.CompanyName.ToUpper(); 所以在存钱的时候我什么都做不了。只有当我从数据库获取数据时,我才需要应用这个过滤器。数据来自\u company.CompanyName 我必须在上面加上过滤器 到目前为止,我已经做到了 公共字符串替
_company.ShortName = _company.CompanyName.ToUpper();
所以在存钱的时候我什么都做不了。只有当我从数据库获取数据时,我才需要应用这个过滤器。数据来自\u company.CompanyName
我必须在上面加上过滤器
到目前为止,我已经做到了
公共字符串替换字符(字符串字)
{
单词=单词。替换(“,”,”);
单词=单词。替换(“;”,“”);
单词=单词。替换(“.”,“”);
单词=单词。替换(“,”);
单词=单词。替换(“,”);
返回单词;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
Show(替换字符(textBox1.Text.ToUpper());
}
提前谢谢。我使用的是C#这里有一个基本的正则表达式,它与您提供的案例相匹配。需要注意的是,正如Kobi所说,您提供的案例是不一致的,所以我从前四个测试中去掉了周期。如果两者都需要,请添加评论 这可以处理您需要的所有案例,但是edge案例的快速增加让我认为您应该重新考虑最初的问题
[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("Company@234 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);
}
[TestMethod]
public void RegexTest()
{
Assert.AreEqual(“私人有限公司”),RegexMethod(“私人有限公司”);
Assert.AreEqual(“公司私人有限公司”,RegexMethod(“公司私人有限公司”);
Assert.AreEqual(“私人有限公司”,RegexMethod(“私人有限公司”);
Assert.AreEqual(“公司私人有限公司”),RegexMethod(“公司私人有限公司”);
Assert.AreEqual(“公司234私人有限公司”,RegexMethod(“Company@234私人有限公司);
//针对新需求的两项新测试
Assert.AreEqual(“公司有限公司”),RegexMethod(“公司有限公司”);
AreEqual(“theasdatheapple”,RegexMethod(“theasdatheapple,,,,the”);
//在这种情况下,一开始就有
AreEqual(“CCC”,RegexMethod(“CCC”);
}
公共字符串RegexMethod(字符串输入)
{
//旧方法先于新要求
//返回Regex.Replace(输入@“The | The |[^A-Z0-9\s]”,string.Empty,RegexOptions.IgnoreCase);
//锚定第一个
//返回Regex.Replace(输入“^The |【^A-Z0-9\s]”,string.Empty,RegexOptions.IgnoreCase);
//第三种方法是对最后一次测试进行回顾和展望
返回Regex.Replace(输入,@“^(The)+\s|\s(?)?
这些是步骤。1和2可以组合,但这更清楚
删除整个单词中的“the”(也适用于“.the”)
删除任何不是字母或空格的内容
移除所有相邻的空间
删除边上的空格
在案例1,2中,结果上有点,但在案例3中删除了点。在本例中失败的是…苹果,,,输出为:Theasdaapple预期输出:Theasdapplekobi键入时出错..它将进行编辑..不应有任何特殊字符。谢谢通知..查看我提供的内容ed-它满足了您的要求,但是有几十种可能的边缘情况“在公司名称的中间,应该删除它们吗?有很多方法让正则表达式迎合大多数需求,但是你需要先明确这些需求。对于测试用例,+1是我假设的,是在实际方法之前写的。不要是不好的,但是我认为这是比一组*.Read()更好的代码。呼叫。另一方面,通过先显示测试用例,那些不习惯这种方法的人就不那么容易理解和理解答案了。+1在几分钟内击败了我,并且给了OP没有提到的边缘案例一个注释,这样我就不用自己写了:)@priyanka-老实说,这是不可能的。你一直在更改规格,或者发明新的规格。试着编辑你的问题,并定义明确的规则。应该删除该
吗?为什么?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("Company@234 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);
}
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"