[英]How can i make a Regex such that is should allow only 2 characters M or F
I would like to valid a string if it consists of M
or F
. 我想验证一个由M
或F
组成的字符串。 If possible i would also like to know if some one enters Male
or Female
i would like to get the result as M
or F
.. But my first case is to check whether the string has M
or F
. 如果可能的话,我也想知道是否有人输入Male
或Female
我想得到的结果是M
或F
.。但是我的第一种情况是检查字符串是否具有M
或F
In any other character i would like to make my result as false. 在其他任何字符中,我都想将结果设为假。
If there are only 4 valid choices, then I reccomend you not use regex. 如果只有4个有效选择,那么我建议您不要使用正则表达式。 You could use an extension method: 您可以使用扩展方法:
public static bool InList<T>(this T item, params T[] list)
{
return list.Contains(item);
}
Then in your code you could use: 然后,您可以在代码中使用:
myString.InList("M", "Male", "F", "Female")
That would return TRUE if it was one of the four choices. 如果它是四个选择之一,则将返回TRUE。 After you validate the gender, you could just grab the first letter using .SubString(). 验证性别后,您可以使用.SubString()抓取第一个字母。
Try this - 尝试这个 -
var match = Regex.Match("M", @"^M(ale)?$|^F(emale)?$");
var result = match.Success;
Use the regexp ^(M|Male|F|Female)$
in ignore-case mode, if it matches, get the first character and uppercase it. 在忽略大小写模式下使用regexp ^(M|Male|F|Female)$
,如果匹配,则获取第一个字符并将其大写。 Precompile the regexp to get an efficient finite state machine. 预编译正则表达式以获得有效的有限状态机。 The result will then be machine code like this: 结果将是这样的机器代码:
if s[0] == 'M':
if s[1] == \0: return True
if s[1] != 'a': return False
if s[2] != 'l': return False
if s[3] != 'e': return False
if s[4] == \0: return True
return False
if s[0] == 'F':
if s[1] == \0: return True
if s[1] != 'e': return False
if s[2] != 'm': return False
if s[3] != 'a': return False
if s[4] != 'l': return False
if s[5] != 'e': return False
if s[6] == \0: return True
return False
return False
Hard to beat this in the number of CPU cycles. 在CPU周期数上很难击败这一点。
I think you are looking at this wrong. 我认为您看错了。
What you need is a drop-down list or a couple of radio-buttons constraining the user's choices and returning exactly the data you want. 您需要一个下拉列表或几个单选按钮来限制用户的选择并准确返回所需的数据。
Otherwise, your users will have problems with your software. 否则,您的用户会遇到软件问题。 Some will make typos, some may type a space after the word, some of them will use a "." 有些人会打错字,有些人可能会在单词后面输入空格,有些人会使用“”。 after M or F because it's an abbreviation. M或F之后的缩写,因为它是缩写。 Additionally, you need to explain that you want them to write M or F. Users don't read, so anything which requires an explanation is an annoyance. 此外,您需要说明您希望他们写M或F。用户不会阅读,因此需要说明的任何内容都令人讨厌。
A question with one answer only out of a few possible answers should be asked with a radio button or a drop down list. 一个问题中只有一个答案的问题应该通过单选按钮或下拉列表提出。
If you really want the user to write M or F, note that none of the previous answers posted allow you to get "m" or "f" as the result if the user writes "Male" or "Female". 如果您确实希望用户写M或F,请注意,如果用户写“ Male”或“ Female”,则先前发布的答案均不允许您获得“ m”或“ f”。 If you go that route, you need something like this: 如果您走那条路线,则需要这样的东西:
your_string.toUpper();
if (your_string.contains("M", "MALE", "F", "FEMALE"))
{
switch (your_string)
{
case "M":
return "M";
case "Male":
return "M";
case "F":
return "F";
case "Female":
return "M";
default:
return false;
}
}
This works, but it's much better to use the right UI tools for the job. 这行得通,但是最好使用正确的UI工具来完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.