C # Fifth job (regular expressions and delegates)

Source: Internet
Author: User

1. Regular expressions

When writing a program or Web page that handles strings, there is often a need to find strings that match certain complex rules. Regular expressions are the tools used to describe these rules. In other words, the regular expression is the code that records the text rule. Regular expressions consist of two types of characters, literals (literal value), metacharacters (metacharacters). The literal value is the character you want to match, and the metacharacters are special characters used by regular expressions, like a command that makes regular expressions function accordingly.

Suppose you look for hi in an English novel , you can use the regular expression hi. This is almost the simplest regular expression, it can exactly match such a string: two characters, the previous character is H, the latter is I. Typically, a tool that handles regular expressions provides an option to ignore the case, and if this option is selected, it can match any of the four cases of Hi, Hi,hi,hi. Unfortunately, many words contain the two consecutive characters of Hi, such as him, history, high, and so on. With hi to find, the side of the hi will be found. If we want to find the word hi exactly, we should use \bhi\b. \b is a special code defined by regular expressions that represents the beginning or end of a word, which is the dividing line of a word. Although English words are usually delimited by spaces, punctuation marks, or line breaks, \b does not match any of these word-delimited characters, it only matches one position.

This is the function of regular expressions, which are used in many text editors to retrieve and replace text that conforms to a pattern.

However, regular expressions are flawed.

When a regular expression contains a qualifier that can accept duplicates, the usual behavior is to match as many characters as possible (in order for the entire expression to be matched).

Sometimes, we need to match as few characters as possible. The qualifier given above can be converted to another mode, just add a question mark after it. So. * is meant to match any number of repetitions, but with minimal repetition in the premise that the entire match succeeds.

2. Delegation

Delegate is a type that represents a reference to a method with a specific argument list and return type.When instantiating a delegate, we can associate its instance with any method that has a compatible signature and return type. We You can invoke a method from a delegate instance. The event handler is the method that is called through the delegate.   We You can create a custom method that can be called by a class (such as a Windows control) when a particular event occurs.

Any method that matches a delegate type in any accessible class or struct can be assigned to a delegate.   This method can be either a static method or an instance method.   This allows you to programmatically change method calls and to insert new code into existing classes.

Delegates have the following properties:

1) Delegates are similar to C + + function pointers, but they are type-safe.

2) The delegate allows the method to be passed as a parameter.

3) Delegates can be used to define callback methods.

4) Delegates can be chained together; For example, multiple methods can be called on an event.

5) The method does not have to match the delegate type exactly.

A delegate is a class that defines a method's type so that it can be passed as a parameter to another method, which dynamically assigns the method to the parameter, avoids the large use of the If-else (Switch) statement in the program, and makes the program more extensible.

A delegate can be defined, and of course it can be banned.

The definition of a delegate is usually similar to a variable, with little difference.

C # Fifth job (regular expressions and delegates)

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.