单字符匹配 [英] single character matching

查看:152
本文介绍了单字符匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想在输入时检查使用验证。
只能输入y或n。
"([y] | [n])"
我试过像但是却无法找到解决方法







































































或"N"或"N"。并且之前的任何内容和之后的任何内容都会使输入无效("是"无效,"无"无效等等),然后使用此模式...

string pattern =" ^ [YyNn]


" ;;

然后你可以做这样的事情...

字符串响应;
while(true)
{
Console.Write(" Blah,Blah。这没关系[y / n]?");
response = Console.ReadLine();
if(Regex.IsMatch(响应,模式) ))
break;

Console.WriteLine("请仅输入Y,y,N或n。");
}
//使用"response"执行某些操作;

hi 
i want to check using validation while inputing.
only y or n can be input.
"([y]|[n])"
i have tried  like this but unable to find the soloution

解决方案

Presuming that the only valid input is "y", "Y", "n" or "N" and that anything before and anything after makes the input invalid ("yes" is invalid "no" is invalid, etc.) then use this pattern...

    string pattern = "^[YyNn]


";

and then you can do something like this...

    string response;
    while(true)
    {
         Console.Write("Blah, Blah.  Is this okay [y/n]? ");
         response = Console.ReadLine();
         if(Regex.IsMatch(response,pattern))
            break;

        Console.WriteLine("Please enter only Y, y, N, or n.");
    }
     // do something with "response"


这篇关于单字符匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆