Basic Principles of Regular Expressions
Regular Expression grammar to understand the principles of regular expressions, You need to first understand some basic knowledge of computer language grammar. A syntax can be defined by a single quad element. G = {Vt, Vn, S, P} indicates a set of non-null and finite symbols, and each element of it becomes the Terminator. Vn is also a collection of non-null and finite symbols. Each element of Vn is called a non-Terminator and Vt 1_vn = Phi. The START symbol of grammar G. P is a finite set of non-null values. Its elements are called generative expressions. The so-called generative formula is in the form of α → β. α is called the left part of the generative formula, and beta is called the right part of the generative formula. The symbol "→" indicates "defined ", in addition, α, βε (Vt 1_vn) *, α =ε, that is, α and β are sign strings composed of Terminator and non-Terminator. The START character S must appear at least once on the left of a certain formula. The language that can be deduced by grammar is labeled as L (G ). Based on the limitations imposed on the generative formula, the famous linguistics Chomsky divides grammar into four types: 0, 1, 2, and 3. Type 0 grammar requires at least one non-Terminator, And there is basically no limit. A very important theoretical result is that the ability of Type 0 grammar is equivalent to that of type 1 Turing Machine, also called context grammar, corresponding to a Linear Bounded automatic machine, each generative α → β is required. | β |> = | α |, | β | indicates the length. Type 2 Grammar is also called context-independent syntax, corresponding to the push-down automatic mechanism, it is required to meet the requirements on the basis of the 1-type grammar: each α → β has a non-terminator Alpha; the 3-type grammar is also called the regular syntax, it corresponds to finite state automation. It satisfies the following requirements on the basis of Type 2 Syntax: A → α | α B (right linear) or A → α | B α (left linear ). A regular expression is the last expression of regular syntax, which uses the entire alphabet as the final set Vt. Assume that the Syntax is {S-> Sa; S-> B;}, then the corresponding regular expression is ba *. Therefore, although the regular expression, regular syntax, and finite state automation refer to different things, they are inherently equivalent. The regular expression is a regular syntax with a limit greater than the context-independent syntax. The programming language syntax we use is a context-independent syntax, so we try to Process Code through a regular expression (such as language translation and code generation) the effort is likely to be futile. However, using the code as plain text and then using regular expressions during processing can still greatly improve the efficiency. The basic operator of a regular expression contains many metacharacters to express rules. However, this article does not introduce how to use regular expressions. The best reference for regular expression rules is "proficient in regular expressions". In fact, there are only the following operators at the core of a regular expression: Name example remarks or operation r | s matches the languages L (r) and L (s) the rs matching languages for the Union set join operations are L (r) and L (s). The r * matching languages are L (r) and L (s) the language matching the brackets (r) is the same as that of L (r). The kleene operator has the highest priority and is left-bound. The join is second, or the operation has the lowest priority. Calculation law: Example note r | s = s | r | the calculation satisfies the exchange law r | s | t = r | (s | t) | satisfies the combination law r (st) the connection can be combined with r (s | t) = rs | rt connection pair | allocable r = r branch is the unit of the connection, r * = (r | Branch) \ * The closure must contain idempotent extension operators that make the regular expression more expressive. Here are just a few examples: extension operator equivalent form + r + = rr * = r * r? R? = R | alias character class [a1a2... An] = a1 | a2 |... | An; if it is a continuous character class, can be written as [a1-an] advanced features: regular expressions have many advanced features, such as capture, view, curing group, etc, these features are designed to improve the practical value of regular expressions and are not within the scope of regular expressions. As mentioned earlier, NFA and DFA correspond to finite state automation and are divided into deterministic finite state automation (DFA) and non-deterministic finite state automation (NFA ), the two state machines have the same capabilities and can recognize Regular Languages. The regular expression recognition engine is constructed based on DFA or NFA. I will not describe the basic theory of the state machine here. As long as I have a slight impression on it, it will not prevent reading. A letter in NFA can mark multiple edges that exit the status, and a token can also mark an edge. This shows that the NFA matching process faces many forks and needs to be selected, if a road failure occurs, you need to repeat it. It is a regular expression (a | B) * NFA corresponding to abb, which is quite intuitive and can basically be directly converted from a regular expression. DFA can only have one edge marked with the letter and leaving the letter in each status and letter in the alphabet. This shows that the DFA matching process is correct, each letter must be matched once. Similar to the above NFA equivalent DFA, for example, it is not intuitive: Converting NFA into DFA because NFA and DFA have the same capabilities, each NFA must be converted into an equivalent DFA. Since DFA determines the status that each input can reach, the set of states that the input string s may reach in NFA corresponds to a state in equivalent DFA. Starting from this idea, DFA can be constructed. First, NFA's initial state 0 does not accept, so we can construct the initial state (0) of DFA; Set (0) input a, can reach (0, 1) in NFA ), therefore, this state is constructed, and the edge from (0) to (0, 1) is marked as a set (0) and input B. The side that can be reached is (0 ), therefore, we construct an edge set marked as B from (0) to itself (0, 1) and input a. What can be reached is (0, 1). It is similar to the previous set (0, 1) input B, which can be (), constructed state (), and corresponding edge set () input a, can reach (), no new state, add an edge set () and input B to construct a new State () Set () and input a to reach ), you can add an edge to the Set () and input B to reach (0). After adding an edge, there is no new state. The final DFA obtained after the end is as follows) it contains the final status 3 of NFA, so it is also the intermediary status of DFA. You can rename the status to get the same DFA as above. The efficiency difference between DFA and NFA is easy to understand. The cost of constructing DFA is much higher than that of NFA. Assuming that the number of NFA states is K, the number of States equivalent to DFA can theoretically reach the power of 2 k, however, there is almost no such extreme situation. It is certain that constructing DFA will consume more time and memory. However, once the DFA is constructed, the execution efficiency is very satisfactory. If the length of a string is n, the execution complexity of the matching algorithm is O (n ); during the matching process, NFA has a large number of branches and rollback. Assume that the number of NFA states is s, because the number of states that each input character may reach is mostly s, then, the complexity of the matching algorithm is multiplied by the length of the input string by the number of States O (ns ). The NFA & DFA construction, conversion, and simplification of regular expressions have a complete set of theories and methods, which are far more complex than the above example. This article uses only a simple example to describe the principles. NFA and DFA differences in capabilities NFA and DFA match algorithms, in addition to the difference in efficiency, from a higher point of view, form the engine of the two styles, in this way, the matching capability of the regular expression varies with other aspects. NFA is called "expression-dominated" engine, while DFA is called "text-dominated" engine. NFA: The expression begins with the first part of the expression. Each time a part is checked, the current text is checked to see if it matches the current part of the expression. If yes, the next part of the expression is continued, until all parts of the expression match, that is, the entire expression matches successfully. Let's look at the expression to (nite | knight | night) matching the text... tonight... process: The first part of the expression is t, which will repeatedly scan until t is found in the string, and then check the subsequent o, check the following elements if they match. In this example, the following elements are (nite | knight | night), which means nite, knight, or night. The engine tries these three possibilities in sequence. In the whole process, control is converted between the elements of the expression, so it is called "expression-dominated ". Expression-dominated is characterized by the fact that each subexpression is independent and does not have an internal link. The hierarchical relationship between subexpressions and the control structure (multiple choice and quantifiers) of the entire regular expression controls the entire matching process. DFA: when the text leads DFA to read a text, it records all currently valid matching expression locations (these location sets correspond to a status of DFA ). The above matching process is used as an example: When the engine reads text t, the matching position of the record is to (nite | knight | night); then the record reads o, match location to (nite | knight | night); read n, match location to (nite | knight | night), two locations, knight eliminated ;... this method is called "text-dominated" because the scanned string controls the engine execution process. One of the differences: NFA expressions affect the NFA expression-dominated feature of the engine and affect the engine by modifying the regular expression. Therefore, although the following three expressions can match the same text, however, the engine execution process is different: to (nite | knight | night) tonite | toknight | tonightto (k? Night | nite) but for DFA, there is no difference. Difference 2: DFA can ensure the longest match for expressions that contain or have options. NFA may report a successful match after successfully matching an option. At this time, it does not know whether the subsequent options will also succeed, whether or not a longer match is included. Assume that one (self) is used )? (Selfsufficient )? To match oneselfsufficient. NFA first matches one and then self. At this time, it is found that selfsufficient cannot match the remaining substring, but this subexpression is not required. Therefore, success can be returned immediately, the matched string is oneself. In fact, the matching results of the NFA engine are related to the specific implementation, and the DFA will certainly successfully match the oneselfsufficient. Difference 3: NFA supports more functions NFA supports "capture group", "surround View", "take precedence quantifiers", "fixed talk group", and other advanced functions, these functions are based on the feature of "child expressions matching independently. However, DFA cannot record the relationship between matching history and subexpressions, and thus cannot implement these functions. It can be seen that the NFA engine has greater practical value. Therefore, the regular expression library we use in programming languages is based on NFA. The Pattern of java is based on NFA. The Pattern. compile () method is obviously constructing the NFA state diagram.