使用正则表达式将字符串映射到功能 [英] Mapping Strings to Functionality Using Regular Expressions

查看:121
本文介绍了使用正则表达式将字符串映射到功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串和多个正则表达式,例如一个正则表达式,它检查字符串是否仅为数字,是否以字符X开头等等.根据匹配的正则表达式,我运行不同的代码,如下所示:

I have a string and multiple regex's, for example a regex that checks if the string is a number only, if it starts with character X and so on. I have different codes running depending on what regex gets matched, like this:

if (Regex.IsMatch(myString, regex1))
{
    //number
}
else if (Regex.IsMatch(myString, regex2))
{
    //something else
}
else if (Regex.IsMatch(myString, regex3))
{
    //something else
}

,依此类推.但是,这看起来很笨拙,因为我要经历10个正则表达式,所以我可以使用switch/case做同样的事情吗?如果是这样,我可以举个例子吗?

and so on. However, this looks very clunky as I have like 10 regex's to go through, so can I do the same thing using switch/case? If so, can I be provided with an example?

我正在使用.NET 2.0和WinForms.

I am using .NET 2.0 and WinForms.

推荐答案

无法按照您的描述进行操作,因为switch只能与以下各项一起使用:bool,char字符串,int,enum或相应的可为null的类型.

This cannot be done as you describe because switch can only be used with: bool, char string, int, enum or a corresponding nullable type.

这篇关于使用正则表达式将字符串映射到功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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