Interpretation of regular expressions in C #

xiaoxiao2021-03-06  89

For many years, many programming languages ​​and tools contain support for regular expressions, and a series of namespaces and a range of classes that make full playback of rule expressions, and they are also with future Perl. The rule expression in 5 is compatible.

In addition, the Regexp class can also complete some other functions, such as editing from the right left binding mode and expression.

In this article, I will briefly introduce the classes and methods in System.Text.RegularExpression, some string matching, and replacement examples, and the details of the group structure, and finally, some of the common you may use. Expression.

Basic knowledge that should be mastered

Knowledge of rules expressions may be one of the knowledge of many programmers "often forget". In this article, we will assume that you have mastered the usage of rule expressions, especially the use of expressions in Perl 5. The .NET's regexp class is a supercharge in the expression in Perl 5, so it is theoretically as a good starting point. We also assume that you have the basic knowledge of the C # syntax and .NET architecture. If you don't have a rule expression, I suggest you start learning from Perl 5's grammar. The authoritative book in rule expressions is a book written by Jeffrey Freder, which we strongly recommend reading this book for readers who wish to understand expressions. RegularExpression assembly

The Regexp rule class is included in the System.Text.RegularExpressions.dll file, you must reference this file when compiling the application, for example:

CSC r: system.text.regulaRexpressions.dll foo.cs

The command will create a foo.exe file, which references the System.Text.RegularExpressions file. Name Space Introduction

Only 6 classes and one definition in the namespace, they are: Capture: The result of one match; CaptureCollection: Capture sequence; group: The result of a group record, inherited by Capture; Match: One expression The matching result is inherited by group; MatchCollection: Match's sequence; Matchevaluator: The agent used when replacing the operation; Regex: Instance of the compiled expression.

There are also some static methods in the Regex class:

Escape: Side escape in Regex in the string; ismatch: If the expression matches in the string, the method returns a Boolean value; Match: Returns the instance of Match; Matches: Return a series of match Method; Replace: Replace the matching expression with replacement strings; split: Returns a series of characters determined by the expression; Unescape: Do not escape the escape character in the string. Simple match

We first start learning from the simple expression of Regex, Match classes. Match m = regex.match ("Abracadabra", "(A | B | R) "); we now have an instance of the MATCH class that can be used to test, for example: if (m.success) ... if Want to use the matching string, you can convert it into a string: console.writeline ("match =" m.toString ()); this example can get the following output: match = abra. This is the matching string. The replacement of the string is very intuitive. For example, the following statement: string s = regex.replace ("Abracadabra", "Abra", "zzzz"); it returns a string zzzcadzzzz, all matching strings are replaced with zzzzz.

Now let's look at an example of a more complex string: string s = regex.replace ("abra", @ "^ / s * (. *?) / S * $", "$ 1"); this statement returns String Abra, its preamble and suffix are removed. The above mode is very useful for deleting leading and subsequent spaces in any string. In C #, we often use alphanuce strings, in an alphanumeric string, the compiler does not treat characters "/" as an escape character. When using characters "/" specifies the escape character, @ "..." is very useful. Also worth mentioning $ 1 is used in a string replacement, it indicates that the replacement string can only contain the alternative string. Detail of the matching engine

Now, we understand a slightly complicated example through a group structure.

Look at the following example: String text = "Abracadabra1abracadabra2abracadabra3"; string Pat = @ "(# first group start ABRA # matching string abra (# second group start CAD # matching string CAD)? # Second Group End (Optional)) # The first group ends # Match once or multiple "; // ignore the comment using X modifier 忽 注 注 Regex R = New Regex (PAT," X "); // Get group number List int [] gnums = r.getGroupNumBers (); // Match Match m = r.match (text); while (m.success) {// From group 1 Start (int i = 1; i

转载请注明原文地址:https://www.9cbs.com/read-107140.html

New Post(0)