使用正则表达式
刚才犯了一个错误,把namespace写成Regex ,结果,和下面的Regex类型冲突了,呵呵!
正则表达式使用很简单,主要是构造表达式:
class Program
{
static void Main(string[] args)
{
string input = "haha@sina.com";
string patten = @"[a-zA-Z]+@[a-zA-Z]+\.com$"; //注意第一个"@"符号,和后面的"\."符号.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Regex r = new Regex(patten);
Match m = r.Match(input);
if (m.Success)
{
Console.WriteLine(m.Value);
}
else {
Console.WriteLine("it's a invalid Email Address !");
}
}
}
很简单,但在patten表达式前面加"@"的原因是:
因为后面要用"\."来表示".",所以,要在最前面加上"@"符号,表示后面的"\"是转义字符.这跟Java还有点不一样,呵呵!
正则表达式使用很简单,主要是构造表达式:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
很简单,但在patten表达式前面加"@"的原因是:
因为后面要用"\."来表示".",所以,要在最前面加上"@"符号,表示后面的"\"是转义字符.这跟Java还有点不一样,呵呵!