The Regular Expression of C # is used to delegate XML. <6>,
C # Regular Expressions
I. Regular Expressions
A regular expression is a string. Do not write a common expression at once. Write it first. If it is incorrect, modify it again.
The regular expression is used to find the regular expression.
Keyword: Regex
-- Introduce namespace System. Text
Common Methods
1. Matching:
-- Regex. IsMatch (string to be matched, regular expression); Determine whether the specified Regular Expression matches the specified string
True is returned if matching, otherwise false is returned.
Console. writeLine ("Enter the zip code"); string regex = @ "^ \ d {6} $"; if (Regex. isMatch (Console. readLine (), regex) {Console. writeLine ("entered correctly");} else {Console. writeLine ("error ");}
2. Extraction:
--> Regex. Match (string to be extracted, regular expression );
Extract the matching characters from the specified Regular Expression in the specified string. Match only extracts the first matching data.
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 extracts all required data,
A MatchCollection set is returned. None of the matching data is an element of the set.
String time = "June 26,195 1"; string regex = @ "(\ w +) \ s * (\ d +), \ s * (\ d + )"; matchCollection mc = Regex. matches (time, regex); foreach (Match m in mc) {Console. writeLine ("month {0} {1}-{2}", m. groups [1]. value, m. groups [2]. value, m. groups [3]. value );}
3. extraction group
() In a regular expression, the meaning of priority is the meaning of priority and grouping. In a regular expression, there is no left parentheses "(", which is divided into groups,
If there is no group, the entire matched result is a group, that is, Groups [0], and Groups [I]. Value is matched.
Group I value of the data
String day = "2012/05/30"; Console. writeLine (Regex. replace (day, @ "(\ d +)/(\ d +)", "$1 year $ February $3 "));
Extract the html code of a webpage
-- WebClient class (import the namespace System.net)
String str = Instance name. DownloadString (IP address); // a string is returned.
You can set the encoding attribute if the extracted result is garbled.
Instance name. Encoding
Greedy mode and non-Greedy Mode
Cancel greedy mode + Add a number after the number?
If you do not cancel the greedy mode, as many matches as possible. If multiple greedy modes appear in an expression, the first matches as many as possible,
All of the following will be changed to non-Greedy by default. When the first mark is non-greedy, the second will greedy the non-greedy
String path = @ "C: \ 154 \ 2FDF \ 3FF \ 4dfgf \ 5dgdgd \ 6gd \ 7dgd \ 8dgg \ 9dg \ 0.txt"; string regex = @"(. + )\\(. + )\\(. +) \ (\ w + \. \ w +) "; // the first one. + is the greedy mode, so it will match as much as possible, so the first one. + It will always match 7dgd \ here, and the second and third. + The default mode is non-Greedy. Match mc = Regex. match (path, regex); // The second matches 8dgg, and the third matches 9dg Console. writeLine ("{0} \ r \ n {1} \ r \ n {2}", mc. groups [1]. value, mc. groups [2]. value, mc. groups [3]. value); Console. readKey ();
Extension: backslash (*****)
In C #, "\" indicates escape, and "\" indicates a diagonal line (in the text) "\\\\" indicates a diagonal line in the regular expression.
In the regular expression, \ Represents escape \ represents a diagonal line (in the regular expression)
C # Delegation
Iv. Delegation
Why is there a commission?
-- Implement callback (that is, the method is registered to the delegate variable, then passed out, and then called outside to execute this method will be called back to the original place to execute this method)
-- Multithreading
-- Custom execution
Difference between delegation and pointer
-- "Delegation" is a type in which delegate variables are used. Delegation is type-safe, and delegation is essentially a class.
-- "Indicates a non-secure code. It is process-oriented and a point of the address.
Keyword: delegate
--> Delegate void delegate type name ();
There are only two access modifiers: public/priveta
Delegation is type-safe
Delegate bool DelegateMFunc (int I); delegate void DelegateFunc (); class Person {public void Func () {Console. writeLine ("Haha") ;}} 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 ("My Func"); return num % 2 = 0? True: false ;}}
A simple case of callback using the delegate implementation method
Namespace _ 09 delegate _ method callback {Form1 // declare a delegate public delegate void DFunc (); public partial class Form1: Form {public Form1 () {InitializeComponent ();} DFunc NewFunc; // define a delegate variable private void button#click (object sender, EventArgs e) {// register NewFunc = MyFunc for the delegate variable; Form2 form2 = new Form2 (NewFunc ); // new A Form2 form. In the constructor, pass the delegate variable to form2.Show (); // pop-up window} // This method is private void MyFunc () registered for the delegate variable () {textBox1.Text = "Hello ";}}}
// Form2
Namespace _ 09 delegate _ method callback {public partial class Form2: Form {public Form2 () {InitializeComponent () ;} DFunc MyDele; // define a delegate variable // write an overload of the constructor. A parameter is used to receive the delegate variable public Form2 (DFunc c) {this. myDele = c; // assign the delegate variable passed by Form1 to InitializeComponent ();} private void button#click (object sender, EventArgs e) {MyDele (); // The delegate variable is used here to implement the callback Form1 function }}}
C # delegation (three-Click Event)
The write event must be delegated.
Namespace _ 13 Three-Click Event {public partial class Form1: Form {public Form1 () {InitializeComponent (); myButton1.MyClik + = new threeButtonDelegate (MyShow);} void myButton1_MyClik () {throw new NotImplementedException ();} private void MyShow () {MessageBox. show ("Haha, I become handsome again! ") ;}} 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
V. XML
XML is equivalent to a small database, but it is saved in txt.
-"Case sensitive
-Only one pair of root nodes can exist.
-"Tags must appear in pairs,
-The start and end of a tag. If there is only one tag, the end must be <test/>.
-The attribute value must be enclosed in quotation marks.
Write // Add a Root node XElement xeRoot = new XElement ("Root"); for (int I = 0; I <10; I ++) {// new: a subnode. Name the node XElement xePerson = new XElement ("Person") in the constructor. // Add the node using the Add method, the parameter indicates the object xeRoot of the root node to be added. 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. setA TtributeValue ("id", I); // assign xeName to the node through Value. value = "zhangsan" + I; xeAge. value = "20"; xeSex. value = "male";} xeRoot. save ("E: \ students. xml "); // save and read // The Loed method to obtain the XML document. The parameter is the path XDocument xDoc = XDocument to obtain the XML document. load (@ "E: \ students. xml "); XElement xeRoot = xDoc. root; // get the Root node name of this XML document, DiGui (xeRoot); // recursive call, pass the Root reception name to the Console. readKey ();} static void DiGui (XElement xe) {// loop root node, foreach (XElement item in xe. elem Ents () {// determine whether it is the last sub-element if (! Item. hasElements) {// obtain the name of the last node and the value Console. writeLine (item. name + "" + item. value);} foreach (XAttribute xa in item. attributes () {Console. writeLine ("attribute: {0} value: {1}", xa. name, xa. value);} // recursive call. If there are still nodes under this node, the cycle continues. Until the last node is known as DiGui (item );}}