Basic. NET Framework knowledge (3)

Source: Internet
Author: User
Tags character classes

1. Regular Expression: Use a string of characters to verify whether a regular expression complies with the regular expression.
2. metacharacters commonly used in regular expressions:
. Match any character except linefeed
\ W matches letters, numbers, underscores, and Chinese characters
\ S matches blank characters
\ D matching number
\ B matches the start or end of a word
^ Match the start of a string
$ End of matching string
Example: string regstr = @ "^ \ d $ ";
Regex reg = new Regex (regstr );
String intputstr = "163 ";
If (reg. IsMatch (intputstr ))
{
Console. WriteLine ("correct ");
}
Else
{
Console. WriteLine ("error ");
}
Note: Add a namespace
3. escape characters in Regular Expressions:
Except for. $ ^ {} [(|) * +? \, Other characters match itself
\ A matches the bell
\ B Escape Character \ B is a special case. In a regular expression, \ B indicates the word boundary (between \ w and \ W). However, in the [] character class, \ B indicates the return character.

In the replacement mode, \ B always indicates the return character.
\ T matches the Tab character
\ R matches the carriage return.
\ V matches with vertical characters
\ F matches with the break
\ N matches the linefeed.
\ E matches the Esc character
\ Matches this character when it is followed by an escape character
Example: string regstr = @ "^ 0 \ d {2}-\ d {8} $ ";
Regex reg = new Regex (regstr );
String intputstr = "010-99999999 ";
If (reg. IsMatch (intputstr ))
{
Console. WriteLine ("correct ");
}
Else
{
Console. WriteLine ("error ");
}
4. Repeat in Regular Expressions:
* Repeated zero or more times
+ Repeat once or more times
? Repeat once or zero times
{N} repeated n times
{N,} repeat n times or more times
{N, m} repeats n to m times, and m cannot be less than n
5. character classes:
[Abcde] matches any character in a, B, c, d, e.
[0-9] It has the same meaning as \ d and matches any number.
[A-z0-9A-Z] The meaning is the same as \ w, matching numbers, letters, underscores
For example, phone number verification
String regstr = @ "^ \(? 0 \ d {2} [)-]? \ D {8} $ ";
Regex reg = new Regex (regstr );
String intputstr = "010-22222222 ";
If (reg. IsMatch (intputstr ))
{
Console. WriteLine ("correct ");
}
Else
{
Console. WriteLine ("error ");
}
Note: ^ start mark
\(? Indicates (0 or once can appear)
0 indicates itself
\ D {2} indicates that the number appears twice
[)-]? In), space,-one character appears 0 times or once
\ D {8} 8 digits
$ End mark
6. Branch condition: There are several rules. If any of these rules is met, they should be regarded as matching. The specific method is to use | to separate different rules.
Example: Phone number verification
String regstr = @ "^ \ (0 \ d {2} \) [-]? \ D {8 }$ | ^ 0 \ d {2} [-]? \ D {8} $ ";
Regex reg = new Regex (regstr );
String intputstr = "(010)-22222222 ";
If (reg. IsMatch (intputstr ))
{
Console. WriteLine ("correct ");
}
Else
{
Console. WriteLine ("error ");
}
Note: The condition is matched from left to right. If the first match is successful, the second condition is no longer matched.
^ \ (0 \ d {2} \) [-]? \ D {8 }$ | ^ match (010)-22222222 or (010) 22222222
^ 0 \ d {2} [-]? \ D {8} $ match 010-22222222 or 010 22222222
7. Grouping expression: You can use a qualifier to duplicate a single character, for example, \ d? Is an integer that appears once or does not appear
String regstr = @ "^ ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \.) {3} ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) $ ";
Regex reg = new Regex (regstr );
String intputstr = "001.020425302.1640 ";
If (reg. IsMatch (intputstr ))
{
Console. WriteLine ("correct ");
}
Else
{
Console. WriteLine ("error ");
}
Note: The IP address ranges from 0 to 255. Each segment has a "." after the first three digits, which can be grouped. Of the three numbers, a hundred digits can only be 0, 1, 2,

When it is 0 or 1, ten digits are allowed. When the number of digits is 2, the number of digits cannot exceed 5. When the number of digits is 5, the number of digits cannot exceed 5,

When the number of digits is 0 and 1, when the number of digits is 2, the number of digits is 5, and the number of digits is 2 and the number of digits is 0 to 4, each digit can be divided into three groups.
8. Negative:
\ W matches any character that is not a letter, digit, or Chinese Character
\ S matches any character that is not a blank character
\ D matches any non-numeric characters
\ B matching is not the start or end position of a word
[^ X] matches any character except x
[^ Aeiou] matches any character except aeiou.
9. Backward reference: if a group exists, each group will automatically have a group number, which can be consecutively arranged from |. You can also name the Group to replace the group number.
For example, string regstr = @ "^ (\ d) (-) \ 1 \ 2 $"; // you can use a name to replace string regstr = @ "^ (? <Gsw> \ d) (-) \ k <gsw> \ 1 $ ";

If the first group is named, the second group number starts from 1. You can also replace <>'
Regex reg = new Regex (regstr );
String intputstr = "1-1 -";
If (reg. IsMatch (intputstr ))
{
Console. WriteLine ("correct ");
}
Else
{
Console. WriteLine ("error ");
}
Note: The group naming format is :(? <Group Name> or (? 'Group name'
Backward direction sometimes \ k <Group Name> or \ k'group name' is used'
10. Zero-width assertion: determines whether certain conditions are met in a certain position.
(1) Zero-width positive prediction first assertion: can match the expression behind the location where the assertions appear
Example: static void Main (string [] args)
{
String regstr = @ "\ B \ w + (? = Ing \ B )";
Regex reg = new Regex (regstr );
String intputstr = "this eating jumping ";
Match mat = reg. Match (intputstr );
Print (mat );
}
Static void Print (Match match)
{
If (match. Value! = "")
{
Console. WriteLine ("matched Value: {0}, matched location: {1}", match. Value, match. Index );
Print (match. NextMatch ());
}
}
Note :? = Match words ending with ing
(2) Assertion after positive review with Zero Width: The expression can be matched in front of the location where the assertions appear
Example: static void Main (string [] args)
{
String regstr = @"(? <= <(A) \ s. *> ).*(? = <\/\ 1> )";
Regex reg = new Regex (regstr );
String intputstr = "<a href = 'HTTP: // cn.bing.com '> Bing </a> ";
Match mat = reg. Match (intputstr );
Print (mat );
}
Static void Print (Match match)
{
If (match. Value! = "")
{
Console. WriteLine ("matched Value: {0}, matched location: {1}", match. Value, match. Index );
Print (match. NextMatch ());
}
}}
Note :? <= Match tags starting with <a> and ending with </a>
(3) Zero-width negative prediction first assertion: assertion the expression behind this position cannot match
Example: static void Main (string [] args)
{
String regstr = @ "\ B \ w * th (?! A) \ w * \ B ";
Regex reg = new Regex (regstr );
String intputstr = "this toothache tooth ";
Match mat = reg. Match (intputstr );
Print (mat );
}

Static void Print (Match match)
{
If (match. Value! = "")
{
Console. WriteLine ("matched Value: {0}, matched location: {1}", match. Value, match. Index );
Print (match. NextMatch ());
}
}
Note :?! Match words with non-expressions
(4) Assertion after review with Zero Width negative: assertion the expression cannot match before this position
Example: static void Main (string [] args)
{
String regstr = @ "\ B \ w *(? <! A) th \ w * \ B ";
Regex reg = new Regex (regstr );
String intputstr = "this toothache tooth ";
Match mat = reg. Match (intputstr );
Print (mat );
}

Static void Print (Match match)
{
If (match. Value! = "")
{
Console. WriteLine ("matched Value: {0}, matched location: {1}", match. Value, match. Index );
Print (match. NextMatch ());
}
}
Note :? <! Match words with non-expressions
11. Notes :? # Comment content
NOTE: Annotations cannot be added to an expression.
12. Categories of Back Reference

13. Exact match and fuzzy match: Fuzzy match starts from the beginning to the end, and the exact match is an end character match closest to the start.
*? Repeat any time, but as few as possible
+? Repeat once or more times, but as few as possible
?? Repeated 0 times or once, but as few as possible
{N, m} repeats n to m times, but should be repeated as few as possible
{N ,}repeated more than n times, but as few as possible
Example: static void Main (string [] args)
{
String regstr = @ "\ bt. * s \ B"; // fuzzy match, @ "\ bt .*? S \ B "; exact match
Regex reg = new Regex (regstr );
String intputstr = "Children eat lunch at an orphanage being used by the U. N. children

'S agency UNICEF for Haitian children separated from parents after last month's earthquake ";
Match mat = reg. Match (intputstr );
Print (mat );
}

Static void Print (Match match)
{
If (match. Value! = "")
{
Console. WriteLine ("matched Value: {0}, matched location: {1}", match. Value, match. Index );
Print (match. NextMatch ());
}
}
14. Generic
(1) using generic types can maximize code reuse, protect type security, and improve performance.
(2) The most common use of generics is to create a collection class.
(3) the. NET Framework class library contains several new Generic collection classes in the System. Collections. Generic namespace.

Use these classes as much as possible to replace common classes, such as the ArrayList in the System. Collections namespace.
(4) you can create your own generic interfaces, generic classes, generic methods, generic events, and generic delegation.
(5) You can restrict generic classes to access methods of specific data types.
(6) Information about types used in generic data types can be obtained through reflection at runtime.
Type parameter naming rules:
(1) use descriptive names to name generic parameters.
(2) Consider using T as the type parameter name with a single letter type parameter
(3) Make sure that T is the prefix of the descriptive type parameter name.
(4) consider specifying the constraints on this type of parameter in the parameter name
Generic method:
Static void Main (string [] args)
{
PrintType <int> (5 );
PrintType <bool> (true );
PrintType <string> ("generic zhenwei ");
PrintType (1.2); // if the type parameter is omitted, the compiler automatically exports data of the double type,

The compiler exports a type parameter based on the parameter type. If the method does not have a parameter, the compiler cannot deduce it.
}
Static void PrintType <T> (T I)
{
Console. WriteLine ("type: {0,-20} value: {1}", I. GetType (), I );
}
Note: The generic method at this time is somewhat similar to the case where the parameter types in the method overload are different and the number is the same. If the generic method is used, the code is greatly reduced.
15. Generic constraints: when defining generic classes, you can impose restrictions on the types of types that client code can use for type parameters when instantiating classes.

If the client code attempts to instantiate a class using a type not allowed by a certain constraint, a compile-time error will occur. The constraint is specified with the where context keyword.
The following are the types of constraints:
(1) T: The structure type parameter must be a value type. You can specify any value types.
(2) T: The class type parameter must be of the reference type. This applies to any class, interface, delegate, or array type.
(3) T: The new () type parameter must be of the reference type. This applies to any class, interface, delegate, or array type.
(4) T: The <Base Class Name> type parameter must be a specified base class or derived from the specified base class.
(5) T: <Interface Name> the type parameter must be a specified interface or implement a specified interface. Multiple interface constraints can be specified. The constraint interface can also be generic.
(6) T: The type parameter provided by U for T must be provided by U or derived from U. This is called the bare type constraint.
Example 1:
T: Structure
The type parameter must be a value type.
Static void Main (string [] args)
{
PrintType <int> (5); // correct syntax
PrintType <string> ("generic really great"); // incorrect syntax
}
Static void PrintType <T> (T I) where T: struct
{
Console. WriteLine ("type: {0,-20} value: {1}", I. GetType (), I );
}
Example 2:
T: Class
The type parameter must be of the reference type. This applies to any class, interface, delegate, or array type.
Static void Main (string [] args)
{
PrintType <int> (5); // incorrect syntax
PrintType <string> ("generic really great"); // the correct method
}
Static void PrintType <T> (T I) where T: class
{
Console. WriteLine ("type: {0,-20} value: {1}", I. GetType (), I );
}
Example 3:
T: new ()
The type parameter must have a public constructor without parameters. When used with other constraints, the new () constraint must be specified at the end.
Class Program
{
Static void Main (string [] args)
{
PrintType <Demo> (new Demo ());
}
Static void PrintType <T> (T I) where T: new ()
{
Console. WriteLine ("type: {0,-20} value: {1}", I. GetType (), I );
}
}
Class Demo
{
}
Example 4:
T: <Base Class Name>
The type parameter must be a specified base class or derived from the specified base class.
Class Program
{
Static void Main (string [] args)
{
PrintType <Demo> (new Demo1 (); // correct
PrintType <Demo> (new Demo (); // correct
PrintType <Demo1> (new Demo1 (); // correct
PrintType <Demo2> (new Demo2 (); // Error
}
Static void PrintType <T> (T I) where T: Demo
{
Console. WriteLine ("type: {0,-20} value: {1}", I. GetType (), I );
}
}
Class Demo
{}
Class Demo1: Demo
{}
Class Demo2
{}
Example 5:
T: <Interface Name>
The type parameter must be a specified interface or implement the specified interface. Multiple interface constraints can be specified. The constraint interface can also be generic.
Class Program
{
Static void Main (string [] args)
{
PrintType <IDemo> (new Demo (); // correct
PrintType <Demo> (new Demo (); // correct
}
Static void PrintType <T> (T I) where T: IDemo
{
Console. WriteLine ("type: {0,-20} value: {1}", I. GetType (), I );
}
}
Interface IDemo
{}
Class Demo: IDemo
{}
Example 6:
T: U
The type parameter provided for T must be provided for U or derived from U. This is called the bare type constraint.
Class Program
{
Static void Main (string [] args)
{
PrintType <IDemo, Demo> (new Demo (); // Error
PrintType <Demo, IDemo> (new Demo (); // correct
PrintType <Demo, Demo> (new Demo (); // correct
}
Static void PrintType <T, U> (T I) where T: U
{
Console. WriteLine ("type: {0,-20} value: {1}", I. GetType (), I );
}
}
Interface IDemo
{}
Class Demo: IDemo
{}
You can also apply multiple constraints to parameters of the same type, and the constraints can be generic.
As follows:

Static void PrintType <T> (T I) where T: class, new ()
{
Console. WriteLine ("type: {0,-20} value: {1}", I. GetType (), I );
}
16. generic class: generic class encapsulation is not specific to operations of specific data types. Generic classes are most commonly used in collections, such as link lists, hash tables, stacks, queues, and trees.

Operations such as adding and removing items from a set are generally performed in the same way, regardless of the type of the stored data. Generally, the process of creating a generic class is as follows:

Change each type to a type parameter one by one from an existing specific class until the optimal balance between generalization and availability is achieved.
Note the following when creating your own generic class:
(1) which types are generic as type parameters.
(2) generally, the more types that can be parameterized, the more flexible the code will be, the better the reusability. However, too much generalization makes it difficult for other developers to read or understand the code.
(3) If there are constraints, what constraints should be applied to type parameters. A useful rule is that the application has as many constraints as possible, but still enables you to process the types that must be processed.

For example, if you know that your generic class is only used for reference type, the application class constraint is applied. This prevents your class from being accidentally used for value types, and allows you to use the as operator for T and check for null values.
(4) Whether to break down generic behaviors into basic classes and subclasses. Since generic classes can be used as base classes, the design considerations here are the same as those for non-generic classes.
(5) Whether to implement one or more generic interfaces.
17. default: This keyword returns null for the reference type and zero for the value type. For the structure, this keyword returns initialization zero or

Each structure member of null depends on whether the structure is a value type or a reference type.
18. Program: define your own generic class
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;

Namespace Day1302
{
Class Program
{
Static void Main (string [] args)
{
MyList <int> list = new MyList <int> ();
List. add (1 );
List. add (2 );
List. add (3 );
List. add (4 );
List. add (5 );
List. remove (3 );
Foreach (int I in list)
{
Console. WriteLine (I );
}

}
}
Class MyList <T>: IEnumerable
{
T [] t = new T [0];
Int count;
Public int Count
{
Get
{
Return count;
}
}
Public T this [int index]
{
Get
{
If (index> count)
{
Throw new Exception ("exceeds the index! ");
}
Else
{
Return t [index];
}
}
Set
{
If (index <count)
{
T [index] = value;
}
Else
{
Throw new Exception ("exceeds the index! ");
}
}
}
Public MyList ()
{}
Public MyList (int capacipy)
{
T = new T [capacipy];
}
Public int Capacipy
{
Get
{
Return t. Length;
}
Set
{
If (value <count)
{
Throw new Exception ("capacity less than the number of elements! ");
}
Else
{
T [] t1 = new T [value];
For (int I = 0; I <count; I ++)
{
T1 [I] = t [I];
}
T1 = t;
}
}
}
/// <Summary>
/// Add
/// </Summary>
/// <Param name = "t2"> </param>
/// <Returns> </returns>
Public int add (T t2)
{
If (t. Length = 0)
{
T = new T [4];
T [0] = t2;
Count ++;
Return 0;
}
Else if (count <t. Length)
{
T [count] = t2;
Count ++;
Return count-1;
}
Else
{
T [] t3 = new T [t. Length * 2];
For (int I = 0; I <count; I ++)
{
T3 [I] = t [I];
}
T3 [count] = t2;
Count ++;
T = t3;
Return count-1;
}
}
/// <Summary>
/// Remove an element
/// </Summary>
/// <Param name = "t2"> </param>
Public void remove (T t2)
{
Int index =-1;
For (int I = 0; I <count; I ++)
{
If (t [I]. Equals (t2 ))
{
Index = I;
}
}
If (index! =-1)
{
For (int I = index; I <count-1; I ++)
{
T [I] = t [I + 1];
}
T [count-1] = default (T );
Count --;
}
}

Public IEnumerator GetEnumerator ()
{
For (int I = 0; I <count; I ++)
{
Yield return t [I];
}
}
}
}

 

 

 

This article from the "Big lazy girl" blog, please be sure to keep this source http://lanyatou.blog.51cto.com/3306130/625828

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.