數往知來C#之 正則表達式 委託 XML<六>

C# 正則表達式篇

1、正則表達式
正則表達式就是一個字符串,不要想着一會兒能夠寫出一個通用的表達式,先寫,不正確再改
寫正則表達式就是在找規律
關鍵字:Regex   
--》引入命名空間  System.Text
經常使用的方法
一、 匹配:
   --》Regex.IsMatch(要匹配的字符串,正則表達式); 判斷指定的正則表達式和指定的字符串是否匹配
若是匹配返回true,不然返回false
 
html

Console.WriteLine("請輸入郵政編碼");
            string regex = @"^\d{6}$";
            if (Regex.IsMatch(Console.ReadLine(), regex))
            {
                Console.WriteLine("輸入正確");
            }
            else
            {
                Console.WriteLine("error");
            }

          

二、 提取: 
  -->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);
            }


三、提取組
()在正則表達式裏的意思就是優先級和分組的意思,在正則表達式裏沒遇到一個左圓括號「(」就分一組,
若是沒有分組那麼整個匹配到的結果就是一組,也就是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#中  \表示轉義,   \\表示一個斜線(在文本中)   \\\\表示正則表達式中的一個斜線
在正則表達式中    \ 表示轉義   \\表示一個斜線(正則表達式中)ide

C# 委託篇

4、委託
爲何要有委託
  --》實現回調  (就是把方法註冊給委託變量,而後傳出去,而後再外面調用執行這個方法的時候會調回原來的地方執行這個方法)
  --》實現多線程
--》自定義執行
委託與指針的區別
  --》委託是一個類型,使用的時候,是在使用委託變量,委託是類型安全的,委託的本質就是類。
  --》指針式非安全代碼,是面向過程的,是地址的一個指向
關鍵字: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;
        }
    }

用委託實現方法回調的一個簡單的案例
ui

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
this

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的函數
        }
    }
}

C#  委託篇(三連擊事件)

寫事件必需要有委託
編碼

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;
            }
        }
    }

C# XML篇

5、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);
            }
        }
相關文章
相關標籤/搜索