正则表达式是个非常重要的工具,最早在Perl等脚本语言中广泛使用。它语法简单,但功能强大,可以从大量的字符串当中快速的筛选出自己想要的内容。
下面列举一些常用的基本的正则表达式,以备查询使用。
一、基本的正则表达式
1、"^\d+$" //非负整数(正整数 + 0)
2、"^[0-9]*[1-9][0-9]*$" //正整数
3、"^((-\d+)|(0+))$" //非正整数(负整数 + 0)
4、"^-[0-9]*[1-9][0-9]*$" //负整数
5、"^-?\d+$" //整数
6、"^\d+(\.\d+)?$" //非负浮点数(正浮点数 + 0)
7、"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数
8、"^((-\d+(\.\d+)?)|(0+(\.0+)?))$" //非正浮点数(负浮点数 + 0)
9、"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数
10、"^(-?\d+)(\.\d+)?$" //浮点数
11、"^[A-Za-z]+$" //由26个英文字母组成的字符串
12、"^[A-Z]+$" //由26个英文字母的大写组成的字符串
13、"^[a-z]+$" //由26个英文字母的小写组成的字符串
14、"^[A-Za-z0-9]+$" //由数字和26个英文字母组成的字符串
15、"^\w+$" //由数字、26个英文字母或者下划线组成的字符串
16、"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email地址
17、"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" //url
18、/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日
19、/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // 月/日/年
20、"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil
21、"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?" //电话号码
22、"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP地址
23、YYYY-MM-DD基本上把闰年和2月等的情况都考虑进去了
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$
二、C#中的一些例子:
1、正则分割字符串
1
2
3
|
string
test =
"XXXX|YYY|ZZZZ"
;
string
[] result = Regex.Split(test,
"[|]"
);
//按照|对原字符串进行分割
result.ToList().ForEach(x => Console.WriteLine(x));
//XXXX,YYY,ZZZZ
|
2、看超链接是否匹配
1
2
3
|
string
str =
"1.asp?id=100"
;
Regex regex =
new
Regex(
@"1.asp\?id\=(\d+)"
, RegexOptions.IgnoreCase);
Console.WriteLine(regex.IsMatch(str));
//True
|
3、筛选内容
1
2
|
Match m = Regex.Match(
@"<span class='blocking-flag'>紧急</span>"
,
@"(?is)(?<=<span[^>]+>).+?(?=</span>)"
);
Console.WriteLine(m.Value);
//紧急
|
4、内容替换
1
2
|
string
result = Regex.Replace(
"数码产品(15),MP3(15),日常用品(12),IT产品(12)"
,
@"\(\d+\)"
,
""
);
Console.WriteLine(result);
//数码产品,MP3,日常用品,IT产品
|
5、去除空格
1
2
3
|
string
result =
" 我 的 中 国 心 "
;
result =
string
.Join(
""
, result.Split(
new
char
[] {
' '
}, StringSplitOptions.RemoveEmptyEntries));
//我的中国心
result = Regex.Replace(result,
@"[\s{1,}]{1,}"
,
""
);
//我的中国心
|
6、请求网址获得链接
1
2
3
4
5
6
7
8
9
10
11
|
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
}
|
7、将字符串的中文部分替换
1
|
Regex.Replace(
"1354444444张,1434324李,王028-4433434"
,
@"[\u4e00-\u9fa5]"
,
""
);
|
至于为什么是\u4e00到\u9fa5,参见:http://zh.wikibooks.org/wiki/Unicode/4000-4FFF。
8、身份证格式验证
1
2
3
4
|
public
static
bool
ValidateIdentitycard(
string
identityCard)
{
return
Regex.IsMatch(identityCard,
@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"
);
}
|
最后附一些相关链接:
正则表达式语言-快速参考 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