Analyze C # files (Updated) with regular expressions

xiaoxiao2021-03-06  93

Analyze C # files (Updated) Jack H Hansen with regular expressions [2004-07-28]

Keywords C # Regular Expression (Regular Expression) syntax highlighting

Many readers must write programs that have been colored by the program code according to the synthesis. And this is a very difficult thing before a period of time. You need to write a lot of code analysis syntax - and this is often the most difficult part. Until, the appearance of regular expression (Regular Expression) can be liberated from heavy work. Regular expressions provide a range of methods (standards, modes) that enable us to efficiently create, compare, and modify strings, and quickly analyze large amounts of text and data to search, remove, and replace text modes [1]. DOTNET Framework offers system.text.RegularExpression namespace to implement their commitments. 1. Regular expression [2] First, I want to briefly introduce the regular expression. Regular expressions were first proposed by mathematician Stephen Klene in 1956. He is proposed on the basis of the increasing research results of natural language. Regular expressions with full syntax use in terms of format matching of characters, later applied to the field of melting information technology. Since then, the regular expression has been developed through several periods, and the current standard has been approved by ISO (International Standards Organization) and is identified by the Open Group organization. Regular expressions are not a dedicated language, but it can be used to find and replace text in a file or character. It has two standards: Basic Regular Expression (BRE), expanding regular expression (ERE). ERE includes BRE function and other concepts. Advanced existing XSH, EGREP, SED, VI, and procedures under UNIX platform implementation of regular expressions. They can be adopted in many languages, such as HTML and XML, which is usually only a subset of the entire standard. As the regular expression is transplanted to the development of the cross platform, its function is also increasingly complete, and the use has gradually widely used. 2. Related expressions related to regular expressions I can only say so much - it is a small knowledge system, it is impossible to explain it clearly. Here I only introduce a matching string associated with C # syntax analysis. For details, see the collection of this Blog Station Regular Expression Specification [The Open Group]. Also, if you have a quite understanding of the regular expression, you can skip the explanation of each of the following to complete the full text as soon as possible.

I> String "(/////?.)*?" Regular expression is removed. $ ^ {[(|) * ? / outside, other characters match itself. In the top of the form, the quotation mark on both sides refers to the quotation marks on both sides of the string. "//" represents a "/" character. "?" Later indicates that match zero or a character. "." Match any character other than / n. "()" Means capturing a matched sub-string. Use () capture to start automatically numbered from 1 according to the order of the left bracket. The first capture of capture element numbered zero is the text that matches the entire regular expression pattern. "*" Behind the brackets indicates that there is one or more such subtrips. That is, "*" is acting on "(// ?.)". "?" The presence makes an empty string can also be captured. II> Crossword string @ "(" |.) *? "Match the string similar to @" hello "" world "!". Match with any term separated by a | (vertical bar) character; for example, Cat | Dog | Tiger. Use the leftmost success match. III> The XML element S * <. *> Match the C # automation XML documentation in the C # document information. "/ S" means any blank character. It should be noted that please do not modify the case at will. Because in the regular expression is sensitive, in its wildcard, the case is often expressed in the opposite. For example, "/ s" means any non-blank character. (The "/ z" below is this) IV> C # document information in content s? * V> blank line ^ / s * / z "^" specifies that the match must appear on the beginning or row of strings. And "/ z" indicates that the specified match must appear before the end of the string or the end of the string ends. Vi> C # Note //.* VII> C # keyword (Abstract | WHERE | While | Yield) {1} (/. | (/ s) |; |, | / (| / [) {1} space Limit, this only lists few keywords (C # has at least 80 keywords ^ _ ^). It should be noted that the parser matches the first success item on the left. Therefore, there is a word containing the relationship Note Sequence: The included person is to be placed before being included. For example: (in | int), it will not find Int, so it should be (int | in). In addition to this, all parentheses (/ { | / [| / (} | /). 3. Related classes [3] [3] [Serializable] Public Class Regex: iSerializable / / Indicates the unality of regular expressions. Regex class contains several static methods Make you can use a regular expression without explicitly create a regex object. Use the static method to equivore the constructing regex object, use the object once again and then destroy it. Thread security. You can create a regex object on any thread and shared between the thread. The above is taken from Microsoft's development document. We also need to use several members:

/ / Search in the specified input string search the regular expression matching item specified in the Regex constructor. Public match match (String INTPUT) for Match classes

[Serializable] public class match: group // represents the result of a single regular expression match. See Microsoft Development Documents for more information on Group. We will use the following member // of the original string to find the starting position from the zero start of the captured sub-string. Public int index {get;} // The length of the sub-string captured. Public int length {get;} // By matching the captured actual sub-string. Public int value {get;} // Gets a value indicating whether the match is successful. Public Bool SuCcess {get;} // Get a collection of groups that match the regular expression. Public Virtual GroupCollection Groups {get;} // From the previous match, the new MATCH containing the next matching result is returned from the last matching position (i.e., the character after the previous matching character). Public match nextmatch (); and the corresponding member of the Group class (in the member of the Match listed above, the first four attributes are inherited by the group class, so these members will not be listed). The matching string must be specified when the instance of the Regex class is initialized. You can use the constructor to create an instance, use it, then destroy it. Or use the static method directly, which is equivalent to creating an instance. However, after testing, I found that static methods should be slightly slower than the compiled regex object. Please see a set of test data below:

4. Write code We now need to analyze the C # language elements listed in the third quarter. What I take is a progressive analysis (if you want to take multi-line analysis, the relevant expression needs to be modified [4]).

Using system.text.regularExpression; // Some Other Codes ... // Create a regex instance (parsing as an example of string). Regex doublequotedstring = new regex ("/"); // then goes to match the string. Match m; for (m = doublequotedstring.match (strsomecodes); m.success; m.nextMatch ()) {foreach (group g in m.groups) {// do some drawings}} The rest is to write coloring code . 5. Source code

Note: [1] "Can ... Text Mode" Putting from the regular expression language element in the .NET Framework General Reference Language Element [2] Regular Expression Introduction About the regular expression of the regular expression Reference from ZDNET China technology and development related information. [3] The signature and comments of the class and functions that appear in this section are from the Microsoft documentation. [4] For more information, please refer to .NET Framework General Reference Regular Expression Language Elements

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

New Post(0)