. 表示除\n之外的任意的单个字符。
[] 字符组,任意的单个字符,中括号中的任意一个字符。
| 表示或的意思。“或”的优先级非常低,最后才计算 |
() 含义:1.改变优先级2.提取组(分组)
限定符:
{n} 表示前面的表达式必须出现n次。
{n,} 表示前面的表达式至少出现n次,最多不限。
{n,m} 至少出现n次,最多出现m次。
{5,10}
* 表示出现0次或多次。{0,}
+ 表示出现1次或多次。{1,}
? 表示0次或1次。 {0,1} ?的另外一个意思是:终止贪婪模式。
简写表达式:
[0-9]
\d 表示 [0-9] 1234567890
\D 表示 [^0-9]
\s 表示所有空白符
\S 表示\s的反面。
\w [a-zA-Z0-9_] 张
\W
^(shift+6):匹配一行的开始。例如正则表达式“^regex”能够匹配字符串“regex我会用”的开始,但是不能匹配“我会用regex”。
^另外一种意思:非!([^0-9])
$ :匹配行结束符。例如正则表达式“浮云$”能够匹配字符串“一切都是浮云”的末尾,但是不能匹配字符串“浮云呀”
teenager
hello welcome to our country.
由于.net默认采用unicode方式来匹配。所以\w也可以匹配汉字。
\b 表示单词的边界。
hello,welcometo our country.thanks
=====================身份证
写法一:^[1-9][0-9]{14}([0-9]{2}[0-9Xx])?$
写法二:^([1-9][0-9]{14}|[1-9][0-9]{16}[0-9X])$
=====================邮箱
@"^[0-9a-zA-Z_.\-]+@[a-zA-Z\-0-9]+(\.[a-zA-Z]+){1,2}$“
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string html= wc.DownloadString("http://localhost:8080/大家留下email交友吧_email_天涯社区.htm");
MatchCollection mathces= Regex.Matches(html, @"[-a-zA-Z0-9_.]+@[a-zA-Z0-9-]+(\.[a-zA-Z]+){1,2}");
========================下载图片
http://gb.cri.cn/42071/2013/11/23/5311s4332512.htm
for(int i = 1; i < 8; i++)
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.GetEncoding("gb2312");
string str = "http://gb.cri.cn/42071/2013/11/23/5311s4332512_"+i+".htm";
string html = wc.DownloadString(str);
MatchCollection match = Regex.Matches(html, "<IMGsrc=\"(/mmsource/images/2013/11/23/([a-zA-Z0-9]+.jpg))\"");
foreach (Match item in match)
{
if (item.Success)
{
string path = "http://gb.cri.cn" + item.Groups[1].Value;
wc.DownloadFile(path, @"G:\大图\" + item.Groups[2].Value);
}
}
}
Console.WriteLine("ok");
Console.ReadKey();
===================================================敏感词处理
小.{0,2}杨.{0,2}
string []lines= File.ReadAllLines("filter.txt",Encoding.Default);
StringBuilder sbmod = new StringBuilder();
StringBuilder sbbanned = new StringBuilder();
for (int i = 0; i < lines.Length; i++)
{
string[]strs= lines[i].Split(new char[]{'='}, StringSplitOptions.RemoveEmptyEntries);
if (strs[1] == "{MOD}")
{
sbmod.Append(strs[0] + "|");
}
else
{
sbbanned.Append(strs[0] + "|");
}
}
sbmod.Remove(sbmod.Length - 1, 1);
sbbanned.Remove(sbbanned.Length - 1, 1);
if (Regex.IsMatch(textBox1.Text.Trim(),sbbanned.ToString()))
{
MessageBox.Show("禁止发帖");
}
else if (Regex.IsMatch(textBox1.Text.Trim(),sbmod.ToString()))
{
MessageBox.Show("审核");
}
else
{
MessageBox.Show("发帖");
}
string[]lines =
File.ReadAllLines(@"网站过滤词(部分).txt",
Encoding.Default);
List<string> listBanned= new List<string>();
foreach(string line in lines)
{
if (line.EndsWith("{BANNED}"))//禁用词
{
string word = Regex.Match(line,@"(.+)=.+").Groups[1].Value;
listBanned.Add(word);
}
}
string pattern = string.Join("|",listBanned.ToArray());//转换成办理商业发票|办理文凭|办理证件|冰毒
if (Regex.IsMatch(textBox1.Text,pattern))
{
MessageBox.Show("禁止发帖");
}
=======================================
WebClientwc= new WebClient();
wc.Encoding= Encoding.UTF8;
string html= wc.DownloadString("http://localhost:8080/美女图片/美女们.htm");
MatchCollectionmat= Regex.Matches(html," <imgalt=\"\" src=\"(.+)\"/>");
foreach(Match item in mat)
{
if (item.Success)
{
string path ="http://localhost:8080/美女图片/"+item.Groups[1].Value;
wc.DownloadFile(path,"G:\\大图\\"+Path.GetFileName(item.Groups[1].Value));
}
}
Console.WriteLine("都加载吧,");
Console.ReadKey();