正则表达式是个非常重要的工具,最早在Perl等脚本语言中广泛使用。它语法简单,但功能强大,可以从大量的字符串当中快速的筛选出自己想要的内容。
下面列举一些常用的基本的正则表达式,以备查询使用。
1、正则分割字符串
\1代表第一个括号分组里的\d+的值,也就是(-|\/|.).
下面列举一些常用的基本的正则表达式,以备查询使用。
1、正则分割字符串
string test = "XXXX|YYY|ZZZZ";
string[] result = Regex.Split(test, "[|]");//按照|对原字符串进行分割
result.ToList().ForEach(x => Console.WriteLine(x));//XXXX,YYY,ZZZZ
2、看超链接是否匹配
string str = "1.asp?id=100";
Regex regex = new Regex(@"1.asp\?id\=(\d+)", RegexOptions.IgnoreCase);
Console.WriteLine(regex.IsMatch(str));//True
3、日期格式是否匹配
string pattern = @"^\d{4}(-|\/|.)\d{1,2}\1\d{1,2}$";
Console.WriteLine("{0} {1} {2} {3}",
Regex.IsMatch("2016.2.2", pattern),//true
Regex.IsMatch("2016.02.02", pattern),//true
Regex.IsMatch("2016-02-02", pattern),//true
Regex.IsMatch("2016/02/02", pattern));//true
\/ 就是/ ,\表示转义.
\1代表第一个括号分组里的\d+的值,也就是(-|\/|.).
4、筛选内容
Match m = Regex.Match(@"<span class='blocking-flag'>紧急</span>", @"(?is)(?<=<span[^>]+>).+?(?=</span>)"); Console.WriteLine(m.Value);//紧急
string s = "34234234@435345&&1||234234@6234235&&12342@564524";
MatchCollection mc = Regex.Matches(s, @"\d+(@\d+)?");
foreach (Match item in mc)
{
//加?输出结果包含1,否则结果为:34234234@435345、234234@6234235、12342@564524
Console.WriteLine(item.Value);
}
string s = @"aaa.com.net.org b.net.org.cn c.org.cn.dd";
MatchCollection mc = Regex.Matches(s, @"\w+.\w+.(?<name>\w+.(org|cn|dd))");
foreach (Match item in mc)
{
//匹配域名 net.org|org.cn|cn.dd
Console.WriteLine(item.Groups["name"].Value);
}
5、内容替换
string result = Regex.Replace("数码产品(15),MP3(15),日常用品(12),IT产品(12)", @"\(\d+\)", "");
Console.WriteLine(result);//数码产品,MP3,日常用品,IT产品
string s1 = "今天的号码是12345689请拨打";
string result = Regex.Replace(s1, @"\d{8,11}", "<a href=\"tel:$0\">$0</a>");
Console.WriteLine(result);//今天的号码是<a href="tel:12345689">12345689</a>请拨打
6、去除空格
string result = " 我 的 中 国 心 ";
result = string.Join("", result.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));//我的中国心
result = Regex.Replace(result, @"[\s{1,}]{1,}", "");//我的中国心
7、请求网址获得链接
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("xxx");//xxx为请求的url
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.Default);
string block = reader.ReadToEnd();//获取返回的html
MatchCollection matches = Regex.Matches(block,@"(?i)<a[^>]*href=([""'])?(?<href>[^'""]+)\1[^>]*>");
foreach (Match match in matches)
{
//match; 获得每个诸如<a href='www.baidu.com'>百度一下</a>
//match.Groups["href"].Value; 获得每个诸如www.baidu.com
}
8、将字符串的中文部分替换
Regex.Replace("1354444444张,1434324李,王028-4433434", @"[\u4e00-\u9fa5]", "");
至于为什么是\u4e00到\u9fa5,参见:http://zh.wikibooks.org/wiki/Unicode/4000-4FFF。
9、身份证格式验证
public static bool ValidateIdentitycard(string identityCard)
{
return Regex.IsMatch(identityCard, @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
}
10、取div块中的内容
string input = "<div class=\"xxxx\">aaaa</div>";
string pattern = "(?is)<div class=\"xxxx\">(?<value>.*?)</div>";
Match match = Regex.Match(input, pattern);
Console.WriteLine(match.Groups["value"].Value);//aaaa
11、去重
string S = "123,12345,14564789,123,456789,456789,454564645,";
var result = string.Join(",", S.TrimEnd(',').Split(',').Distinct());
Console.WriteLine(result);//123,12345,14564789,456789,454564645
最后附一些相关链接:
正则表达式语言-快速参考 http://msdn.microsoft.com/zh-cn/library/vstudio/az24scfc(v=vs.110).aspx
正则表达式语法 http://msdn.microsoft.com/zh-cn/library/ae5bf541(v=vs.100).aspx