一、正則表達(dá)式
正則表達(dá)式就是一個字符串,不要想著一下子可以寫出一個通用的表達(dá)式,先寫,不正確再改
寫正則表達(dá)式就是在找規(guī)律
關(guān)鍵字:Regex
--》引入命名空間 System.Text
常用的方法
1、 匹配:
--》Regex.IsMatch(要匹配的字符串,正則表達(dá)式); 判斷指定的正則表達(dá)式和指定的字符串是否匹配
如果匹配返回true,否則返回false
Console.WriteLine("請輸入郵政編碼"); string regex = @"^/d{6}$"; if (Regex.IsMatch(Console.ReadLine(), regex)) { Console.WriteLine("輸入正確"); } else { Console.WriteLine("error"); }
2、 提取:
-->Regex.Match(要提取的字符串,正則表達(dá)式);
在指定的字符串中提取指定的正則表達(dá)式匹配的字符, Match只提取第一個匹配到的數(shù)據(jù)。
string regex = @"^(http|ftp):///w+(/./w+)+//w+/./w+/?/w+=/w+(&/w+=/w+)*"; string ip = "modaorong@QQ.com"; string regex = @"/w+@(/w+/./w+)/.*"; Match m = Regex.Match(IP, regex); Console.WriteLine(m.Groups[1].Value);
--》Regex.Matchs
Matchs提取所有需要的數(shù)據(jù),
會返回一個MatchCollection集合,沒一個匹配上的數(shù)據(jù)就是這個集合的一個元素
string time = "June 26, 1951"; string regex = @"(/w+)/s*(/d+),/s*(/d+)"; MatchCollection mc = Regex.Matches(time, regex); foreach (Match m in mc) { Console.WriteLine("月{0} 日{(diào)1} 年{2}", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value); }
3、提取組
()在正則表達(dá)式里的意思就是優(yōu)先級和分組的意思,在正則表達(dá)式里沒遇到一個左圓括號“(”就分一組,
如果沒有分組那么整個匹配到的結(jié)果就是一組,也就是Groups[0], Groups[i].Value 就是匹配到的
數(shù)據(jù)的第i組的值
string day = "2012/05/30"; Console.WriteLine(Regex.Replace(day, @"(/d+)/(/d+)/(/d+)", "$1年$2月$3日"));
提取網(wǎng)頁的html代碼
--》WebClient類 (導(dǎo)入命名空間 System.net)
string str=實(shí)例名.DownloadString(IP地址); //會返回一個字符串
提取的結(jié)果亂碼可以設(shè)置encoding屬性
實(shí)例名.Encoding
貪婪模式與非貪婪模式
取消貪婪模式 +號后面加個?
如果不取消貪婪模式就會盡可能多的匹配,如果一個表達(dá)式里出現(xiàn)多個貪婪模式,那么第一個就會盡可能多的匹配,
后面的全都會默認(rèn)的變成非貪婪,當(dāng)?shù)谝粋€標(biāo)記為非貪婪模式那么第二個就會貪婪后面的非貪婪
string path = @"C:/154/2FDF/3FF/4dfgf/5dgdgd/6gd/7dgd/8dgg/9dg/0.txt"; string regex = @"(.+)//(.+)//(.+)//(/w+/./w+)";//第一個.+是貪婪模式,所以會從盡可能多的匹配,所以第一個.+會一直匹配到7dgd/這里,而第二個第三個.+此時就默認(rèn)為非貪婪模式, Match mc= Regex.Match(path, regex); //第二個會匹配8dgg,第三個匹配9dg Console.WriteLine("{0}/r/n{1}/r/n{2}",mc.Groups [1].Value ,mc.Groups [2].Value ,mc.Groups [3].Value ); Console.ReadKey();
擴(kuò)展: 反斜線(*****)
在C#中 /表示轉(zhuǎn)義, //表示一個斜線(在文本中) ////表示正則表達(dá)式中的一個斜線
在正則表達(dá)式中 / 表示轉(zhuǎn)義 //表示一個斜線(正則表達(dá)式中)
四、委托
為什么要有委托
--》實(shí)現(xiàn)回調(diào) (就是把方法注冊給委托變量,然后傳出去,然后再外面調(diào)用執(zhí)行這個方法的時候會調(diào)回原來的地方執(zhí)行這個方法)
--》實(shí)現(xiàn)多線程
--》自定義執(zhí)行
委托與指針的區(qū)別
--》委托是一個類型,使用的時候,是在使用委托變量,委托是類型安全的,委托的本質(zhì)就是類。
--》指針式非安全代碼,是面向過程的,是地址的一個指向
關(guān)鍵字:delegate
-->delegate void 委托類型名();
訪問修飾符只有兩個:public/PRiveta
委托是類型安全的
delegate bool DelegateMFunc(int i); delegate void DelegateFunc(); class Person { public void Func() { Console.WriteLine("哈哈"); } } class Program { static void Main(string[] args) { //Person p = new Person(); //DelegateFunc DFunc; //DFunc = p.Func; //DFunc(); DelegateMFunc MyFuncs; MyFuncs = MyFunc; bool b= MyFuncs(20); Console.WriteLine(b); Console.ReadKey(); } static bool MyFunc(int num) { Console.WriteLine("我的Func"); return num % 2 == 0 ? true : false; } }
用委托實(shí)現(xiàn)方法回調(diào)的一個簡單的案例
namespace _09委托_方法回調(diào){ Form1 //聲明一個委托 public delegate void DFunc(); public partial class Form1 : Form { public Form1() { InitializeComponent(); } DFunc NewFunc;//定義一個委托變量 private void button1_Click(object sender, EventArgs e) { //給委托變量注冊 NewFunc = MyFunc; Form2 form2 = new Form2(NewFunc);//new一個Form2窗體,在構(gòu)造方法中把委托變量傳過去 form2.Show();//彈窗 } //這個方法是給委托變量注冊的 private void MyFunc() { textBox1.Text = "Hello"; } }}
//Form2
namespace _09委托_方法回調(diào){ public partial class Form2 : Form { public Form2() { InitializeComponent(); } DFunc MyDele;//定義一個委托變量 //寫一個構(gòu)造函數(shù)的重載,有一個參數(shù) 用來接收Form1傳過來的委托變量 public Form2(DFunc c) { this.MyDele = c;//把Form1傳過來的委托變量賦給上面定義好的委托變量 InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MyDele();//這里使用委托變量實(shí)現(xiàn)了回調(diào)Form1的函數(shù) } }}
寫事件必須要有委托
namespace _13三連擊事件{ public partial class Form1 : Form { public Form1() { InitializeComponent(); myButton1.MyClik += new threeButtonDelegate(MyShow); } void myButton1_MyClik() { throw new NotImplementedException(); } private void MyShow() { MessageBox.Show("哈哈,我又變帥了!"); } }} public delegate void threeButtonDelegate(); class MyButton:Button { public event threeButtonDelegate MyClik; int i = 0; protected override void OnClick(EventArgs e) { i++; if (i==3) { if (MyClik !=null) { MyClik(); } i=0; } } }
五、XML
XML就相當(dāng)于一個小型的數(shù)據(jù)庫,只不過是以txt來保存
-》大小寫敏感
-》只可以有一對根節(jié)點(diǎn)
-》標(biāo)簽必須成對出現(xiàn),
-》標(biāo)簽有開始必須有結(jié)束,如果只有一個標(biāo)簽,也要有<test/>結(jié)束
-》屬性賦值時要用引號引起來
寫入 //添加一個根節(jié)點(diǎn) XElement xeRoot = new XElement("Root"); for (int i = 0; i < 10; i++) { //new一個子節(jié)點(diǎn),在構(gòu)造方法里面給節(jié)點(diǎn)命名 XElement xePerson = new XElement("Person"); //用Add方法添加,參數(shù)是要添加到哪個根節(jié)點(diǎn)就傳哪個根節(jié)點(diǎn)的對象 xeRoot.Add(xePerson); XElement xeName = new XElement("Name"); xePerson.Add(xeName); XElement xeAge = new XElement("Age"); xePerson.Add(xeAge); XElement xeSex = new XElement("Sex"); xePerson.Add(xeSex); xePerson.SetAttributeValue("id", i); //通過Value給節(jié)點(diǎn)賦值 xeName.Value = "張三" + i; xeAge.Value = "20"; xeSex.Value = "男"; } xeRoot.Save("E://students.xml"); //保存讀取 //Loed方法獲得XML文檔,參數(shù)是要獲得XML文檔的路徑 XDocument xDoc= XDocument.Load(@"E:/students.xml"); XElement xeRoot =xDoc.Root;//獲得這個XML文檔的根節(jié)點(diǎn)名, DiGui(xeRoot);//遞歸調(diào)用,把根接待你名傳過去 Console.ReadKey(); } static void DiGui(XElement xe) { //循環(huán)根節(jié)點(diǎn), foreach (XElement item in xe.Elements()) { //判斷是否是最后一層子元素 if (!item.HasElements) { //得到最后一層節(jié)點(diǎn)的名稱和里面的值 Console.WriteLine(item.Name +" "+item.Value ); } foreach (XAttribute xa in item.Attributes()) { Console.WriteLine("屬性:{0} 值:{1}", xa.Name, xa.Value); } //遞歸調(diào)用,如果這個節(jié)點(diǎn)下面還有節(jié)點(diǎn)就再繼續(xù)循環(huán),知道最后一個節(jié)點(diǎn)為止 DiGui(item); } }
新聞熱點(diǎn)
疑難解答