一、正則表達式
正則表達式就是一個字符串,不要想著一下子可以寫出一個通用的表達式,先寫,不正確再改
寫正則表達式就是在找規律
關鍵字:Regex
--》引入命名空間 System.Text
常用的方法
1、 匹配:
--》Regex.IsMatch(要匹配的字符串,正則表達式); 判斷指定的正則表達式和指定的字符串是否匹配
如果匹配返回true,否則返回false
Console.WriteLine("請輸入郵政編碼"); string regex = @"^/d{6}$"; if (Regex.IsMatch(Console.ReadLine(), regex)) { Console.WriteLine("輸入正確"); } else { Console.WriteLine("error"); }
2、 提取:
-->Regex.Match(要提取的字符串,正則表達式);
在指定的字符串中提取指定的正則表達式匹配的字符, Match只提取第一個匹配到的數據。
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提取所有需要的數據,
會返回一個MatchCollection集合,沒一個匹配上的數據就是這個集合的一個元素
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} 日{1} 年{2}", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value); }
3、提取組
()在正則表達式里的意思就是優先級和分組的意思,在正則表達式里沒遇到一個左圓括號“(”就分一組,
如果沒有分組那么整個匹配到的結果就是一組,也就是Groups[0], Groups[i].Value 就是匹配到的
數據的第i組的值
string day = "2012/05/30"; Console.WriteLine(Regex.Replace(day, @"(/d+)/(/d+)/(/d+)", "$1年$2月$3日"));
提取網頁的html代碼
--》WebClient類 (導入命名空間 System.net)
string str=實例名.DownloadString(IP地址); //會返回一個字符串
提取的結果亂碼可以設置encoding屬性
實例名.Encoding
貪婪模式與非貪婪模式
取消貪婪模式 +號后面加個?
如果不取消貪婪模式就會盡可能多的匹配,如果一個表達式里出現多個貪婪模式,那么第一個就會盡可能多的匹配,
后面的全都會默認的變成非貪婪,當第一個標記為非貪婪模式那么第二個就會貪婪后面的非貪婪
string path = @"C:/154/2FDF/3FF/4dfgf/5dgdgd/6gd/7dgd/8dgg/9dg/0.txt"; string regex = @"(.+)//(.+)//(.+)//(/w+/./w+)";//第一個.+是貪婪模式,所以會從盡可能多的匹配,所以第一個.+會一直匹配到7dgd/這里,而第二個第三個.+此時就默認為非貪婪模式, 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();
擴展: 反斜線(*****)
在C#中 /表示轉義, //表示一個斜線(在文本中) ////表示正則表達式中的一個斜線
在正則表達式中 / 表示轉義 //表示一個斜線(正則表達式中)
四、委托
為什么要有委托
--》實現回調 (就是把方法注冊給委托變量,然后傳出去,然后再外面調用執行這個方法的時候會調回原來的地方執行這個方法)
--》實現多線程
--》自定義執行
委托與指針的區別
--》委托是一個類型,使用的時候,是在使用委托變量,委托是類型安全的,委托的本質就是類。
--》指針式非安全代碼,是面向過程的,是地址的一個指向
關鍵字: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; } }
用委托實現方法回調的一個簡單的案例
namespace _09委托_方法回調{ 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窗體,在構造方法中把委托變量傳過去 form2.Show();//彈窗 } //這個方法是給委托變量注冊的 private void MyFunc() { textBox1.Text = "Hello"; } }}
//Form2
namespace _09委托_方法回調{ public partial class Form2 : Form { public Form2() { InitializeComponent(); } DFunc MyDele;//定義一個委托變量 //寫一個構造函數的重載,有一個參數 用來接收Form1傳過來的委托變量 public Form2(DFunc c) { this.MyDele = c;//把Form1傳過來的委托變量賦給上面定義好的委托變量 InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MyDele();//這里使用委托變量實現了回調Form1的函數 } }}
寫事件必須要有委托
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就相當于一個小型的數據庫,只不過是以txt來保存
-》大小寫敏感
-》只可以有一對根節點
-》標簽必須成對出現,
-》標簽有開始必須有結束,如果只有一個標簽,也要有<test/>結束
-》屬性賦值時要用引號引起來
寫入 //添加一個根節點 XElement xeRoot = new XElement("Root"); for (int i = 0; i < 10; i++) { //new一個子節點,在構造方法里面給節點命名 XElement xePerson = new XElement("Person"); //用Add方法添加,參數是要添加到哪個根節點就傳哪個根節點的對象 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給節點賦值 xeName.Value = "張三" + i; xeAge.Value = "20"; xeSex.Value = "男"; } xeRoot.Save("E://students.xml"); //保存讀取 //Loed方法獲得XML文檔,參數是要獲得XML文檔的路徑 XDocument xDoc= XDocument.Load(@"E:/students.xml"); XElement xeRoot =xDoc.Root;//獲得這個XML文檔的根節點名, DiGui(xeRoot);//遞歸調用,把根接待你名傳過去 Console.ReadKey(); } static void DiGui(XElement xe) { //循環根節點, foreach (XElement item in xe.Elements()) { //判斷是否是最后一層子元素 if (!item.HasElements) { //得到最后一層節點的名稱和里面的值 Console.WriteLine(item.Name +" "+item.Value ); } foreach (XAttribute xa in item.Attributes()) { Console.WriteLine("屬性:{0} 值:{1}", xa.Name, xa.Value); } //遞歸調用,如果這個節點下面還有節點就再繼續循環,知道最后一個節點為止 DiGui(item); } }
新聞熱點
疑難解答