C # Regular Expression (5): Class Introduction 1 under namespace system. Text. regularexpressions

Source: Internet
Author: User
Tags regex replace
Document directory
  • Classes
  • Example 1
  • Example 2
In the namespace regularexpressions, there are 10 classes, one proxy and one Enumeration type classes.
  Class Description
Capture Represents the results from a single subexpression capture. Capture represents one substring for a single successful capture.
Capturecollection Represents a sequence of capture substrings. capturecollection returns the set of captures done by a single capturing group.
Group Group represents the results from a single capturing group. A capturing group can capture zero, one, or more strings in a single match because of quantifiers, sogroup supplies a collection of capture
Objects.
Groupcollection Represents a collection of captured groups. groupcollection returns the set of captured groups in a single match.
  Match Represents the results from a single regular expression match.
  Matchcollection Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.
RegEx Represents an immutable regular expression.
  Regexcompilationinfo Provides information about a regular expression that is used to compile a regular expression to a stand-alone assembly.
  Regexrunner Infrastructure. The regexrunner class is the base class for compiled regular expressions.
  Regexrunnerfactory Infrastructure. Creates a regexrunner class for a compiled regular expression.
Delegates
  Delegate Description
  Matchevaluator Represents the method that is called each time a regular expression match is found during a replace method operation.
Enumerations
  Enumeration Description
  Regexoptions Provides enumerated values to use to set Regular Expression options.

The main class in these classes is actually RegEx, and other classes are used in the use of methods in the RegEx class.

There are many static methods in the RegEx class. We can directly call those methods without instantiating a RegEx class. At the same time, the RegEx class also has the same instance method as the static method function.

That is to say, you can instantiate RegEx before using it.

Whether to call the static method directly or instantiate it first, but calling the instance method is not much different, but there is a slight difference when passing parameters. as for the way you like it, you can enjoy it.

However, in general, if the regular static expression is used only a few times, it is convenient to use the static method. if regular expressions are frequently used, the instantiated class may have better performance. the following two simple examples are provided to illustrate the differences between static and instance methods.

Static Method:

String STR = "csdn.net/weiwenhp ";

String Pattern = "(? <= /).*(? = HP) "; // match the character between/and HP

Bool exist = RegEx. ismatch (STR, pattern); // verify whether the match is successful.

String result = RegEx. Match (STR, pattern). value; // The matched value.

If (exist)

Console. writeline (result); // The result is weiwen.

Instance method:

String STR = "csdn.net/weiwenhp ";

String Pattern = "(? <= /).*(? = HP )";

RegEx Reg = new RegEx (pattern); // instantiate a RegEx class

Bool exist = reg. ismatch (STR); // The usage here is basically the same as that of the static method, except that the parameter pattern is used during instantiation. this parameter is no longer needed here.

String result = reg. Match (STR). value;

If (exist)

Console. writeline (result); // The result is also weiwen

 

The following describes the RegEx replace method and proxy matchevaluator.

 

We know that regular expressions are mainly used for character verification, extraction, segmentation, and replacement. The replace function is used for replacement.

The replace function has four overload functions.

1) Replace (string input, string pattern, string replacement) // input is the source string, pattern is the matching condition, and replacement is the replacement content, is to convert the content that meets the matching condition pattern into it

For example, string result = RegEx. Replace ("ABC", "AB", "#"); // The result is # C, which is to replace AB in string ABC ##

2) Replace (string input, string pattern, string replacement, regexoptions options) // regexoptions is an enumeration type for some settings.

// Regexoptions. ignorepatternwhitespace is used in the preceding annotations. If case sensitivity is ignored during matching, regexoptions. ignorecase can be used.

For example, string result = RegEx. Replace ("ABC", "AB", "#", regexoptions. ignorecase );

If it is a simple replacement, use the above two functions. however, if there are some complicated replicas, such as matching a lot of content, different content should be replaced with different characters. the following two functions are required:

3) Replace (string input, string pattern, matchevaluator evaluator); // evaluator is a proxy. In fact, it is simply a function pointer and a function is used as a parameter.

// Use a proxy to implement similar functions without a pointer in C #. You can use a proxy-bound function to specify the complex replacement you want to implement.

4) Replace (string input, string pattern, matchevaluator evaluator, regexoptions options); // The functions above are the same, except that an enumeration type is added to specify whether to ignore case sensitivity settings.

Let's take a look at a simple example.

Example 1

If Arwen appears twice in a string, replace the first occurrence with weiwen (1), and the second occurrence with weiwen (2)

Public static int I = 0;

Static void main (string [] ARGs)

{

String source = "# Arwen *** & Arwen ###";

String Pattern = "Arwen ";

String result;

Matchevaluator me = replace; // declare a proxy and bind a function. In fact, it is equivalent that me is a pointer to the function replace.

Result = RegEx. Replace (source, pattern, me); // You can also regard me as a function replace to be transmitted as a parameter.

Console. writeline (result); // The result is "# weiwen (1) *** & weiwen (2 )###";

}

Public static string Replace (match Ma)

{

If (MA. Success)

{

I ++;

Return "weiwen" + String. Format ("({0})", I );

}

Return "null ";

}

However, after reading the above code, I still feel a little dizzy. I don't know the sequence in which the code is executed from start to end and what logic is used.

Result = RegEx. Replace (source, pattern, me); // This seems like a sentence. In fact, there are many complicated operations in it and there are also repeated operations.

// Match matchcollection matchs = RegEx. Matches (source, pattern) in this way first. Because it matches two Arwen S, there will be two values in the matchs set.

// Matchs [0]. value = "Arwen"; matchs [1] = "Arwen ". then, the for (INT I = 0; I <matchs. matchs. count; I ++)

//{

// Matchs [I] = Replace (matchs [I]); // Replace the string here

//}

Of course, what I mentioned above is just a rough explanation of the general process behind it.

In implementation, we only use result = RegEx. Replace (source, pattern, me); this sentence is OK.

 

Example 2

 

The above is to match the same character. Let's take another example to show how to match different content and make different replacements.

If there is a number 2 in the string, replace it with God, and replace it with hell if there is a number 3.

String source = "aa2bb3cc ";

String Pattern = @ "\ D ";

String result;

Matchevaluator me = replace;

Result = RegEx. Replace (source, pattern, me );

Console. writeline (result); // The result is aago dbbhellcc.

Public static string Replace (match Ma)

{

Switch (MA. value)

{

Case "2 ":

Return "God ";

Break;

Case "3 ":

Return "hell ";

Break;

Default:

Return "null ";

Break;

}

}

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.