[Regular Expressions] basic knowledge, basic knowledge of Regular Expressions
I have always known the purpose of a regular expression, but I have never used it. Today I have learned a little about its basic usage. Here is a summary.
Command Space: System. Text. RegularExpressions
Purpose:
RegEx class:
Is the class abstraction of the Regular Expression Engine. By calling different methods of this abstract class, you can process the regular expression.
The main method of this class is as follows. You can find the specific purpose by Using the Method Name:
After learning about the main methods of the Regex class, you also need to pay attention to three important classes and their layers: Match Class, Group class, and Capture class.
Assume there are the following strings: input (original string) and pattern (regular expression ).
Match Class: A result returned by a pattern match, such as calling Regex. the Match () method matches the input Regular Expression (pattern) in the string (input). A Match class instance is returned for each matching result.
Group Class: Indicates the matching result of the Child expression in the regular expression (pattern), which is enclosed in parentheses, such as patter = @ "(\ d {}) [.,]? (\ D {1, 3 })". There are two groups: (\ d {1, 2}) and (\ d {1, 3 }).
Capture class: Returns the result of matching the subexpression. CaptureCollections returns all results that match the subexpression.
Difference between Group class and Capture class:
Refer to the help documentation (http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.group (v = vs.110). aspx) on MSDN to illustrate their differences through the following code:
1 using System; 2 using System.Text.RegularExpressions; 3 4 public class Example 5 { 6 public static void Main() 7 { 8 string pattern = @"(\b(\w+?)[,:;]?\s?)+[?.!]"; 9 string input = "This is one sentence. This is a second sentence.";10 11 Match match = Regex.Match(input, pattern);12 Console.WriteLine("Match: " + match.Value);13 int groupCtr = 0;14 foreach (Group group in match.Groups)15 {16 groupCtr++;17 Console.WriteLine(" Group {0}: '{1}'", groupCtr, group.Value);18 int captureCtr = 0;19 foreach (Capture capture in group.Captures)20 {21 captureCtr++;22 Console.WriteLine(" Capture {0}: '{1}'", captureCtr, capture.Value);23 }24 } 25 }26 }27 // The example displays the following output: 28 // Match: This is one sentence. 29 // Group 1: 'This is one sentence.' 30 // Capture 1: 'This is one sentence.' 31 // Group 2: 'sentence' 32 // Capture 1: 'This ' 33 // Capture 2: 'is ' 34 // Capture 3: 'one ' 35 // Capture 4: 'sentence' 36 // Group 3: 'sentence' 37 // Capture 1: 'This' 38 // Capture 2: 'is' 39 // Capture 3: 'one' 40 // Capture 4: 'sentence'
If quantifiers (quantifiers) is added after the character, for example, in the pattern expression, (\ w + ?) Used to match multiple words in a sentence. However, the Group object only returns the last match (\ w + ?) . The Capture records every matching (\ w + ?) . If no quantifiers are added, Capture and Group can be used to represent the same object.
Represents the inheritance relationship between the three (left), and the hierarchy during use (right ):
Next, we will introduce how to write regular expressions. This is the process of classifying and abstracting different strings. It mainly includes the following nine types:
After understanding the relationships between these types, you can use regular expressions to have a clear idea and concept. The key to regular expressions is how to write correct matching statements, which is the basis for other subsequent operations. For details about the Type format, refer to the Quick Reference in PDF (workflow) format in MSDN. (Download link: http://msdn.microsoft.com/en-us/library/hs600312.aspx ).
The following are some simple regular expressions for your exercises to help you understand them intuitively:
1. Replace the first letter with the upper limit:
1 string input = "this is a Good mornIng. "; 2 string pattern = @" \ B ([a-z]) ([a-zA-Z] {2,}) \ B "; 3 string result = Regex. replace (input, pattern,
Match => match. groups [1]. value. toUpper () + match. groups [2]. value); 4 Console. writeLine (result); 5 6 // result: This is a Good MornIng.
2. Replace currency symbols:
1 string word = "$12.4 $0.2 $2 3.5";2 string sub = "$1";3 string pattern = @"\p{Sc}*(\d+[,.]?\d*)";4 string result = Regex.Replace(word, pattern, "($1)");5 Console.WriteLine(result);6 7 //Result:8 //(12.4) (0.2) (2) (3.5)