由于最近网络不好,打不开博客园的网站,上两篇笔记也是托朋友帮发表的,今天由于时间不多,就直接把今天课上笔记直接复制了,不手写了
============================
//正则表达式是个什么东西,干嘛用的?
是个字符串,匹配,提取,替换字符串
2,常见元子符有哪些
3,C#中常见正则表达式的使用方法有哪些
4,什么是贪婪模式
+或*时尽可能的匹配 取消贪婪模式加?。
5,处理匹配的时候,C#中常见类与方法有哪些
Regex.IsMatch Regex.Match Regex.Matches
---------------------------------------------------
1正则表达式 命名空间 System.Text.RegularExpressions
常用的类
Regex Match MatchCollection Group GrpupCollection
常用的方法 Regex.IsMatch() Regex.Match() Regex.Matches() Regex.Replace()
贪婪模式:不要首先考虑他,当出现匹配项比预想的要多的时候,再去考虑
-----------上午-------------------------------------------------------------
截取图片地址下载图片
找到网页上的Src=" "地址
WebClient wc=new WebClient();//创建网络流
String html=wc.DownloadString(@"http://192.168.1.100");
List<string> list=new List<string>();
MatchCollection ms=Regex.Matches(html,@"src=""(.+?)""");
foreach(Match m in ms)
{
if(m.Success)
{
list.Add(m.Groups[1].Value.Trim());
}
}
//与网页地址拼接得到图片的具体地址
string url=@"http://192.168.1.100";
for(int i=0;i<list.Count;i++)
{
string temp=list[i];
temp=url+"/"+temp;
list[i]=temp;
}
//下载
Console.WriteLine("请输入存储路径");
string dir=Console.ReadLine();
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
for(int i=0;i<list.Count;i++)
{
string name=Regex.Match(list[i],@".+/(.+)").Groups[1].Value
wc.DownloadFile(list[i],Path.Combine(dir,name));
}
//实际使用中记得使用try catch
--------------------------------------------------------------------
处理敏感词 Regex.Replace
第一步,读取txt文件
string str=File.ReadAllText("1.txt",Encoding.Default);
第二步,处理敏感词,全部为********
---- 方法1
Match m=Regex.Replace(str,@"罢餐|罢工|四川");
if(m.Success)
{
string temp=new string('*',m.Length);
}
--- 方法2
str=Regex.Replace(str,@"罢餐|罢工|四川","**");
写到文件中
File.WriteAllText("2.txt",str);
-------------------------------------------------------------------
组替换
string str=@"2012年12月21日,是一个很好的日子,是玛雅人预示的毁灭之年",这一年的最后一个月即将结束;
---传统方法
Match m=Regex.Matcch(str,@"(\d+)年(\d+)月(\d+)日");
string temp=string.Format("{0}-{1}-{2}",m.Groups[1].Value,m.Groups[2].Value,m.Groups[3].Value);
---正则表达式里的方法 在Replace方法中使用"$+数字"表示分组
str=Regex.Replace(str,@"(\d+)年(\d+)月(\d+)日","$1-$2-$3-");
------------------------------------------------------------------
HTML压缩混淆
压缩
string str=File.ReadAllText("1.html",Encoding.Default);
str=Regex.Replace(str,@"\s+"," ");
混淆
string str=File.ReadAllText("2.txt");
List<string> varable=new List<string>();
string[] strs={"namespace","bool","string","int","double","char"};
//获得所有变量名
for(int i=0;i<strs.Length;i++)
{
MatchCollection ms=Regex.Matches(str,string.Format(@"{0}\s*(\w+)",strs[i]);
foreach(Match m in ms)
{
if(m.Success)
{
if(!varable.Contains(m.Groups[1].Value))
{
varable.Add(m.Groups[1].Value);
}
}
}
}
//开始混淆
Random r=new Random();
for(int i=0;i<varable.Count;i++)
{
int temp=r.Next();
string nextVal=string.Format("{0}{1}",temp%2==0?"_":"0",temp);
str=Regex.Replace(str,varable[i],nextVal);
}
File.WriteLine("newFile.cs",str,Encoding.Default);
============下午====================================================
练习抓取职位信息
string url=".......好长啊....";
List<string> obj=new List<string>();
WebClient wc=new WebClient();
wc.Encoding=Encoding.Default;
string regex=@"";
string html=wc.DownloadString(url);
//处理
MatchCollection ms=Regex.Matches(html.regex);
foreach(Match m in ms)
{
if(m.Success)
{
Console.WriteLine(m.Groups[1].Value);
}
}
-------------------------------
委托 特征 1,自定义执行 2,实现回调 3,实现多线程
委托是一种类型,将方法当作变量的一种机制
---------------
声明一个委托类型 委托的返回类型必须和方法的返回类型一致
//public delegate 返回类型 委托名(参数)
public delegete void FuncDelegate();
static void Func()
{
Console.WriteLine(" dsdf");
}
Main函数
FuncDelegate wo;
wo=Func;
wo();
----------------- 实例方法的委托
public delegate void FuncDelegate();
Class BanZhang
{
public void Func()
{
Console.WriteLine(" dsdf");
}
}
BanZhang bz=new BanZhang();
FuncDelegate wo;
wo=bz.Func;
wo();
-------------------
委托的一些细节说明
先定义方法,将方法复制过来,改一改方法名即可
委托名一般是方法名加上Delegate或者Delegate或方法名
一般系统定义的委托都是以_开头,以Handle结尾
授权的方法: 1,直接等号 委托变量=方法名 2,new一个委托对象 委托变量=new 委托类型(方法名)
----------- 委托变量的传值处理
public delegate void FuncDelegate();
public static void Func(FuncDelegate Func1)
{
if(Func1!=null)
{
Func1();
}
}
public static void Function()
{
Console.WriteLine("我是方法Function")
}
FuncDelegate MyFunc=Function;
Func(MyFunc);
------------------自定义排序 ------------------------------------
冒泡排序
int[] nums = { 1212, 344, -4556, 3x2, 24, -67, 43, 78};
for(int i=0;i<nums.Length-1;i++)
{
for(int j=0;j<nums.Length-i-1;j++)
{
if(nums[i]>nums[i+1])
{
int n1=nums[j];
nums[j]=nums[j+1];
nums[j+1]=n1;
}
}
}
---------------- 原始的字符串排序 (比较的是字符的ACIS码)
int a=string.Compare(s1,s2);
s1>s2= 1 s1=s2=0 s1<s2=-1
string[] nums = { "1212", "344", "-4556", "3x2", "24", "-67", "43", "78" };
for(int i=0;i<nums.Length-1;i++)
{
for(int j=0;j<nums.Length-1-i;j++)
{
if(string.Compare(nums[j],nums[j+1])>0)
{
string temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
------------------- 长度排法Length
for(int i=0;i<nums.Length-1;i++)
{
for(int j=0;j<nums.Length-i-1;j++)
{
if(nums[j].Length>nums[j+1].Length)
{
string temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
-------------- 用字符串中出现的第一个数字进行排序,如果没有数字,则默认为0
public static bool MyConpare(string s1,string s2)
{
int num1=0;
int num2=0;
Match n1=Regex.Match(s1,@"-?\d+");
Match n2=Regex.Match(s2,@"-?\d+");
if(n1.Success)
{
num1=Convert.ToInt32(n1.Value);
}
if(n2.Success)
{
num2=Convert.ToInt32(n2.Value);
}
return num1>num2?true:false;
}
string[] nums = { "1xx212", "34xx34", "xxx-4556", "32", "24", "-67xxx6", "4xxx3", "7xx8" };
for (int i = 0; i < nums.Length - 1; i++)
{
for (int j = 0; j < nums.Length - i - 1; j++)
{
if (MyCompair3(nums[j], nums[j + 1]))
{
string temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
------------委托的使用,自定义执行-------------
定义委托
public delegate bool CpDelegate(string s1,string s2);
首先定义多种方法
public static bool MyCompare1(string s1,string s2) //字符串比较
{
return string.Compare(s1,s2)>0;
}
public static bool MyCompare2(string s1,string s2)//长度比较
{
return s1.Length>s2.Length;
}
public static bool MyCompare2(string s1,string s2)//比较字符串中出现的数字大小.如果没有默认为0
{
int num1=0;
int num2=0;
Match n1=Regex.Match(s1,@"-?\d+");
Match n2=Regex.Match(s2,@"-?\d+");
if(n1.Success)
{
num1=Convert.ToInt32(n1.Value);
}
if(n2.Success)
{
num2.Convert.ToInt32(n2.Value);
}
return num1>num2?true:false;
}
创建比较方法
public static void Compare(string[] nums,CpDelegate Func)
{
for(int i=0;i<nums.Length-1;i++)
{
for(int j=0;j<nums.Length-i-1;j++)
{
if(Func(nums[j],nums[j+1]))
{
string temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
}
主函数程序
string[] nums = { "1xx212", "34xx34", "xxx-4556", "32", "24", "-67xxx6", "4xxx3", "7xx8" };
CpDelegate MyFunc;
Console.WriteLine("请问用什么方式排序1字符串,2长度,3数字");
string str=Console.ReadLine();
switch(str)
{
case "1":MyFunc=new CpDelegate(MyCompare1);break;
case "2":MyFunc=new CpDelegate(MyCompare2);break;
case "3":MyFunc=new CpDelegate(MyCompare3);break;
default:MyFunc=MyCompare1;break;
}
Compare(nums,MyFunc);
for(int i=0;i<nums.Length;i++)
{
Console.WriteLine(nums[i]);
}
-----------------------------------------------
回调,在类的外面调用类的私有方法(可查看7月6日第十一天老师的代码)
---------------
多线程
开启线程使用System.Threading.Thread类
static void Func1()
{
for(int i=0;i<100000;i++)
{
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine(i);
Console.ForegroundColor=ConsoleColor.White;
}
}
static void Func2()
{
for(int i=0;i<100000;i++)
{
Console.ForegroundColor=ConsoleColor.Yellow;
Console.WriteLine(i);
Console.ForegroundColor=ConsoleColor.White;
}
}
主函数
Thread t1=new Thread(Func1);
Thread t2=new Thread(FUnc2);
t1.Start();
t2.Start();