C# 在搜索列表c之前规范化输入字符串#
我正在创建一个多表单应用程序,该应用程序在用户保存表单之前进行了一系列验证测试。其中一个验证函数需要评估用户是否输入了某些关键字,如“未知”、“TBA”、“N/A”。我需要从规范化输入字符串开始,以去除所有不需要的字符(如空格)和额外的大小写(如“未知”、“TBA”)。然后需要对照我的C# 在搜索列表c之前规范化输入字符串#,c#,.net,regex,winforms,C#,.net,Regex,Winforms,我正在创建一个多表单应用程序,该应用程序在用户保存表单之前进行了一系列验证测试。其中一个验证函数需要评估用户是否输入了某些关键字,如“未知”、“TBA”、“N/A”。我需要从规范化输入字符串开始,以去除所有不需要的字符(如空格)和额外的大小写(如“未知”、“TBA”)。然后需要对照我的列表检查此项 这是我当前的方法,但是我的正则表达式没有正确地规范化我的输入字符串,如果单词前面有空格,它将通过验证 public bool useUnkownEntity(string strTest) {
列表检查此项
这是我当前的方法,但是我的正则表达式没有正确地规范化我的输入字符串,如果单词前面有空格,它将通过验证
public bool useUnkownEntity(string strTest)
{
Regex rgx = new Regex(@"^[a-zA-Z]");
List<string> unkown = new List<string> { "Unkown", "tba", "tbc","N/a"};
useUnkownEntity(rgx.Replace(strTest, ""));
if (unkown.Contains(strTest, StringComparer.OrdinalIgnoreCase))
{
MessageBox.Show("please refre to the unkown ENtity within");
return false;
}
return true;
}
到目前为止,这是我的答案,它忽略了大小写而不是空格,如果我在单词的开头输入一些空格,它将失败,就像在N/a中忘记a/一样
public bool useUnkownEntity(string strTest)
{
Regex rgx = new Regex("[^a-zA-Z/s/ ]");
List<string> unkown = new List<string> { "Unkown", "tba", "tbc","N/A"};
strTest = rgx.Replace(strTest, "");
if (unkown.Contains(strTest, StringComparer.OrdinalIgnoreCase))
{
MessageBox.Show("Please refer to the unkown Entity within!!");
return false;
}
return true;
}
public bool useUnkownEntity(字符串strTest)
{
正则表达式rgx=新正则表达式(“[^a-zA-Z/s/]”);
列表未知=新列表{“未知”、“待定”、“待定”、“待定”、“不适用”};
strTest=rgx.Replace(strTest,“”);
if(unknown.Contains(strTest、StringComparer.OrdinalIgnoreCase))
{
MessageBox.Show(“请参考!!”中的未知实体);
返回false;
}
返回true;
}
也许您想编写新正则表达式(@“[^a-zA-Z]”)
以匹配除ASCII字母以外的任何字符。我尝试过,但现在它给我一个空执行选项,副本上的答案也不是我需要的,我需要考虑N/a等符号,以确定您需要什么,不清楚您需要什么。您是否试图修改strTest
?那么为什么要使用useUnkownEntity(rgx.Replace(strTest,”)代码>?我猜应该是strTest=rgx.Replace(strTest,”)代码>你的方法似乎是递归的-没有退出递归-你能展示你的真实代码吗。
public bool useUnkownEntity(string strTest)
{
Regex rgx = new Regex("[^a-zA-Z/s/ ]");
List<string> unkown = new List<string> { "Unkown", "tba", "tbc","N/A"};
strTest = rgx.Replace(strTest, "");
if (unkown.Contains(strTest, StringComparer.OrdinalIgnoreCase))
{
MessageBox.Show("Please refer to the unkown Entity within!!");
return false;
}
return true;
}