asp.net正则表达式的使用

转载:原文在这

正则表达式是个非常重要的工具,最早在Perl等脚本语言中广泛使用。它语法简单,但功能强大,可以从大量的字符串当中快速的筛选出自己想要的内容。
下面列举一些常用的基本的正则表达式,以备查询使用。

注意:C#,asp.net 使用正则表达式要引入命名空间:System.Text.RegularExpressions
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

下面的asp.net验证正则表达式原文在这:http://www.cnblogs.com/ecollab/p/6144538.html

基本元字符
. 任意的一个非换行字符
[] 集合匹配,匹配一个[]中出现的字符. 是在多个字符中取一个. 
() 调整优先级的作用. 还有一个分组的作用
| 或的意思,测试|一下. 注意,或的优先级最低

限定元字符
+ 紧跟这个符号前面的元素出现1次到多次 {1,}
* 紧跟这个符号前面的元素出现0次到多次 {0,}
? 紧跟这个符号前面的元素有或没有 {0,1}
{n} 紧跟这个符号前面的元素出现n次
{n,} 紧跟这个符号前面的元素出现n次,最多无限次
{n,m} 紧跟这个符号前面的元素出现n次,最多m次

首尾元字符
\d+ "abc123def" "123"
^ 必须以某某字符开头的含义,在[]表示否定 [^abc]  例如:^\d+(必须以数字开头)
$ 必须以某某结尾   例如:\d+$(必须以数字结尾),^\d+$(必须以数字开头和结尾)
$ 分组后对组的引用   

简写元字符(小写肯定,大写否定)
\d 数字 \D 非数字  digit 
\w 文本 \W 非文本  word
\s 空格 \S 非空格  space

asp.net 验证正则表达式

整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$
只能输入数字:"^[0-9]*$"。
只能输入n位的数字:"^\d{n}$"。
只能输入至少n位的数字:"^\d{n,}$"。
只能输入m~n位的数字:。"^\d{m,n}$"
只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。
只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。
只能输入非零的正整数:"^\+?[1-9][0-9]*$"。
只能输入非零的负整数:"^\-[1-9][]0-9"*$。
只能输入长度为3的字符:"^.{3}$"。
只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。
只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。
只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。
只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。
只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"。
验证用户密码:"^[a-zA-Z]\w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。
验证是否含有^%&'',;=?$\"等字符:"[^%&'',;=?$\x22]+"。
只能输入汉字:"^[\u4e00-\u9fa5]{0,}$"
验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。
验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
验证电话号码:"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX- XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。
验证身份证号(15位或18位数字):"^\d{15}|\d{18}$"。
验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。
验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。

猜你喜欢

转载自blog.csdn.net/qq_41987694/article/details/81151555