这篇文章主要介绍了c#使用正则表达式的小示例,匹配字符串、验证URL,大家参考使用吧
在System.Text.RegularExpression命名空间里,有正则表达式方法。
代码如下:
namespace RegexDemo
{
class Program
{
static void Main(string[] args)
{
Regex regex = new Regex("China", RegexOptions.IgnoreCase);
//使用Match方法。
string source = "China is my mother,My mother is china!";
Match m = regex.Match(source);
if (m.Success)
{
Console.WriteLine("找到第一个匹配");
}
Console.WriteLine(new string('-',9));
//下面的样例将演示使用Matches方法进行匹配
MatchCollection matches=regex.Matches(source);
foreach(Match s in matches)
{
if(s.Success)
Console.WriteLine("找到了一个匹配");
}
Console.ReadLine();
}
}
}
[/code]
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有