您的位置:首页 > 编程语言 > C#

C#中的正则表达式的一些细节

2014-05-05 11:01 176 查看
在C#中用正则表达式需要引用using System.Text.RegularExpressions,在写正则表达式时要先写一个@

例如 string email = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";不然提示转义出现错误

string emailinput = Console.ReadLine();

判断是否匹配时,bool match = Regex.IsMatch(emailInput , email)

//Regex.IsMatch(string input , string pattern);注意参数的顺序。

代码:
namespace test2
{
class Program
{
static void Main(string[] args)
{
string email = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";//
while (true)
{
Console.Write("input:");
string emailInput = Console.ReadLine();
bool match = Regex.IsMatch(emailInput, email);
if (match)
{
Console.WriteLine("legal");
}
else
{
Console.WriteLine("illegal");
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: