A construction method, device, storage medium and equipment of a regular expression engine

By merging NFA state jump relationships of continuous strings in the regular expression engine and constructing init-NFA, the problem of excessive state number and jump times is solved, which improves execution efficiency and reduces computing resource consumption.

CN114896469BActive Publication Date: 2025-08-01ALIBABA (CHINA) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210476217.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-04-29
Publication Date
2025-08-01
Estimated Expiration
2042-04-29

AI Technical Summary

Technical Problem

When existing regular expression engines process complex or long regular expressions, the number of states and the number of state jumps are too high, resulting in low execution efficiency and high computing resource consumption.

Method used

By marking continuous ordinary characters in regular expressions as continuous strings and combining their corresponding NFA state jump relationships, an initial non-determined finite automata (init-NFA) is constructed to reduce the unnecessary number of states and jump relationships.

Benefits of technology

Improves the execution efficiency of the regular expression engine and reduces the consumption of computing resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114896469B_ABST
    Figure CN114896469B_ABST
Patent Text Reader

Abstract

An embodiment of this specification provides a method, apparatus, storage medium, and device for constructing a regular expression engine. The method includes: traversing each character of a regular expression, and marking consecutive ordinary characters in the regular expression as a consecutive string; constructing an initial non-deterministic finite automaton based on the regular expression, where the NFA state transition relationships corresponding to the characters marked as the consecutive string are merged into the same NFA state transition relationship. When constructing an NFA, in the embodiment of this specification, by merging the transition relationships corresponding to consecutive ordinary characters marked as a consecutive string into the same transition relationship, the number of unnecessary states and the number of transition relationships are reduced. Therefore, compared with the existing NFA construction methods, the NFA constructed by the method in the embodiment of this specification has higher execution efficiency, and the computing resources consumed during its construction are also relatively low.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of this specification relate to the field of computer technologies, and in particular, to a method, apparatus, storage medium, and device for constructing a regular expression engine. Background Art

[0002] In the prior art, regular expression engines are generally divided into two categories: NFA (Non-deterministic finite automaton) and DFA (Deterministic finite automaton). Generally speaking, an NFA is constructed by sequentially traversing each character in a regular expression string, and the number of states in the NFA is proportional to the length of the regular expression string. A DFA is generated by conversion based on the NFA. Compared with an NFA, since for each input character in a DFA, its corresponding result is determined, the DFA generally has a relatively fast processing speed. However, since a DFA is generated by conversion based on an NFA and needs to traverse all the active state sets of the NFA, the space complexity of the DFA is extremely high. In particular, when the regular expression string is relatively complex or has a long length, the execution efficiency of the DFA will be greatly affected. Therefore, most regular expression engines using a DFA are provided with a self-protection mechanism for error fallback to an NFA.

[0003] In a regular expression, characters can be divided into two types: ordinary characters and special characters (or "meta-characters"). Among them, ordinary characters can be letters (for example, any one of the lowercase letters a - z, or any one of the uppercase letters A - Z), digits (for example, any one of 0 - 9), Chinese characters, or other characters except special characters, while special characters include \, ^, $, *, +, etc. Ordinary characters in a regular expression are used to match strings, while special characters represent special matching behaviors. For example, * represents matching the previous sub-expression zero or more times.

[0004] In a regular expression, if there are consecutive ordinary characters, whether it is an NFA or a DFA generated by converting the NFA, the states before and after the sub-expression composed of these consecutive ordinary characters are generally not affected by any ordinary character in the sub-expression. However, in existing regular expression engines, whether using an NFA or a DFA, for consecutive ordinary characters, they are traversed one character at a time, and each additional character adds one more state, resulting in a high number of states, so that the execution efficiency of the regular expression engine is relatively low and the computing resources are relatively high. Summary of the Invention

[0005] To overcome the problems existing in the related art, an embodiment of this specification provides a method, apparatus, storage medium, and device for constructing a regular expression engine to solve the defects in the related art.

[0006] According to the first aspect of the embodiment of this specification, a method for constructing a regular expression engine is provided. The method includes:

[0007] Traverse each character of the regular expression, and mark consecutive ordinary characters in the regular expression as consecutive strings;

[0008] Construct an initial non-deterministic finite automaton based on the regular expression, where the NFA state transition relationships corresponding to the characters marked as the consecutive strings are merged into the same NFA state transition relationship.

[0009] According to the second aspect of the embodiment of this specification, an apparatus for constructing a regular expression engine is provided. The apparatus includes:

[0010] A character recognition module for traversing each character of the regular expression and marking consecutive ordinary characters in the regular expression as consecutive strings;

[0011] An engine construction module for constructing an initial non-deterministic finite automaton based on the regular expression, where the NFA state transition relationships corresponding to the characters marked as the consecutive strings are merged into the same NFA state transition relationship.

[0012] According to the third aspect of the embodiment of this specification, a computer-readable storage medium is provided, on which a computer program is stored. When the program is executed by a processor, the method described in any of the above embodiments is implemented.

[0013] According to the fourth aspect of the embodiment of this specification, a computer device is provided, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, the method described in any of the above embodiments is implemented.

[0014] In the above technical solution, when constructing an NFA in an embodiment of this specification, by merging the transition relationships corresponding to consecutive ordinary characters marked as consecutive strings into the same transition relationship, the number of unnecessary states and the number of transition relationships are reduced. Therefore, compared with the existing NFA construction methods, the NFA constructed by the method in the embodiment of this specification has higher execution efficiency, and the computing resources consumed during its construction are also relatively low.

[0015] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the embodiments of this specification. Description of the Drawings

[0016] Figure 1 It is a flowchart of a method for constructing a regular expression engine shown in an embodiment of this specification.

[0017] Figure 2 It is a state diagram of a traditional NFA constructed based on the regular expression "a*bcc" shown in an embodiment of this specification.

[0018] Figure 3 It is a state diagram of an init-NFA constructed based on the regular expression "a*bcc" shown in an embodiment of this specification.

[0019] Figure 4 It is a schematic flowchart of constructing a regular expression engine shown in an embodiment of this specification.

[0020] Figure 5 It is a block diagram of a device for constructing a regular expression engine shown in an embodiment of this specification.

[0021] Figure 6 It is a schematic diagram of the hardware structure of a computing device shown in an embodiment of this specification. Detailed implementation manners

[0022] Here, exemplary embodiments will be described in detail, and examples thereof are shown in the drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The implementation manners described in the following exemplary embodiments do not represent all implementation manners consistent with the embodiments of this specification. On the contrary, they are merely examples of devices and methods consistent with some aspects of the embodiments of this specification as detailed in the appended claims.

[0023] The terms used in the embodiments of this specification are only for the purpose of describing specific embodiments and are not intended to limit the embodiments of this specification. The singular forms "a", "the", and "said" used in the embodiments of this specification and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term "and / or" as used herein refers to and includes any or all possible combinations of one or more of the associated listed items.

[0024] It should be understood that although the terms first, second, third, etc. may be used in the embodiments of this specification to describe various information, such information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of the embodiments of this specification, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the word "if" as used herein may be interpreted as "when" or "while" or "in response to determining".

[0025] A regular expression is a text pattern used to describe a set of string characteristics and can be used to match specific strings. In many technical fields, such as natural language processing, data storage, etc., through text pattern matching, regular expressions can conveniently extract target information. Regular expressions can verify text with a specific format and filter out text objects that do not meet the predetermined format. For example, usernames and passwords on websites generally have specific formats, and regular expressions can be used to verify usernames and passwords when submitting forms. Regular expressions can also match specified text through preset conditions and can quickly extract specified content from a large amount of information. For example, find a specified URL from a batch of URLs (Uniform Resource Locators). Regular expressions can also find text with a specified format through matching and perform specific text replacement after finding the text with the specified format. Due to the fast and accurate effect of regular expressions in processing formatted text, regular expressions are currently integrated into various text editors and text processing tools.

[0026] Generally speaking, regular expressions describe patterns through ordinary characters and special characters to achieve the purpose of text matching.

[0027] Ordinary characters in regular expressions consist of all printable and non-printable characters that are not explicitly specified as special characters. This includes all uppercase and lowercase letter characters, all digits, all punctuation marks, and some symbols. In a regular expression, when matching a string, an ordinary character is used to match the same character. The simplest regular expression is a single ordinary character, which can match that character itself in the target string. For example, the single-character pattern "a" can match the letter "a" that appears anywhere in the target string. Combining multiple individual ordinary characters together can result in a larger expression that can match that expression itself in the target string. It should be noted that the matching result of consecutive ordinary characters in a regular expression in the target string is also consecutive ordinary characters, and the order of the characters in the matching result is the same as the order of the characters in the regular expression. For example, the multi-character pattern "abc" can match any occurrence of the substring "abc" in the target string, and moreover, the three letters of the substring "abc" matched in the target string are consecutive, not scattered. Also, the substring matched in the target string should be the substring that satisfies the order of "abc", rather than a substring with other orders, such as "acb".

[0028] Special characters in regular expressions, or metacharacters, enable regular expressions to have processing capabilities. Special characters are those dedicated characters with special meanings in regular expressions, which can be used to specify the occurrence pattern of their leading characters (i.e., the characters preceding the special characters) in the target string. For example, the metacharacter * is used to match zero or more leading characters; while the metacharacter. is used to match any single character except the newline character. More commonly used metacharacters include: "+", "*", and "?". Among them, the "+" metacharacter specifies that its leading character must appear continuously one or more times in the target object, the "*" metacharacter specifies that its leading character must appear zero or continuously multiple times in the target object, and the "?" metacharacter specifies that its leading object must appear zero or once continuously in the target object.

[0029] In this field, the tool for implementing the matching function of regular expressions is called a regular expression engine. The regular expression engine can receive and recognize a given regular expression, and compile and generate a recognition device based on this regular expression. When any string needs to be matched with this regular expression, only need to input this string into this recognition device, and this recognition device can output the corresponding matching result, that is, whether this string can match this regular expression. In existing regular expression engines, generally, finite automata (Finite Automata, FA) are used as recognition devices. A finite automaton is a recognizer that can identify and judge each input character to determine the final state or set of states and paths it can reach. Finite automata can be divided into two categories, namely deterministic finite automata (Deterministic Finite Automata, DFA) and non-deterministic finite automata (Nondeterministic Finite Automata, NFA), that is, regular expression engines can also be divided into two categories, DFA engines and NFA engines.

[0030] A finite automaton (which can be a DFA or an NFA) consists of five parts, which are: a finite state set, an alphabet, a transition function, an initial state, and one or more final states. Among them, the finite state set contains a finite number of states, which are used to represent all possible states from the initial state to all final states in the finite automaton. A state can be regarded as any sub-expression that may appear during the regular expression matching process. For example, if a regular expression is "abcc", the finite state set in the finite automaton generated based on this regular expression can be expressed as {S0, S1, S2, S3, S4}, where S0, S1, S2, S3, and S4 correspond to the states with expressions I, "a", "ab", "abc", and "abcc" respectively, where I represents the initial state, that is, the state without any characters. It should be noted that the expressions corresponding to two different states may be the same. The alphabet is a finite set of all input symbols in the regular expression. For example, in the above example where the regular expression is "abcc", the alphabet is {a, b, c}. It should be noted that the input symbols recorded in the alphabet only include the ordinary characters in the regular expression, and do not include the special characters in the regular expression. For example, the symbol table in the finite automaton with the regular expression "ab|c" is {a, b, c}, which does not include the special character "|". In fact, the special characters in the regular expression are not used as input characters during the construction of the finite automaton, but are used to determine the jump relationship between states. The transition function is used to represent the jump relationship that a certain state will jump to a specified successor state after inputting a specified input character. For example, in the above example where the regular expression is "abcc", one of the transition functions is: S0×a→S1, which means that state S0 will jump to state S1 after inputting the character a. There are also other transition functions in the above example, which will not be elaborated one by one in the embodiments of this specification. The initial state refers to the state of the finite automaton before any characters are input, and this state does not contain any characters. For example, in the above example where the regular expression is "abcc", the initial state is S0, that is, I. The final state refers to all states in the finite automaton that can match the regular expression. When reaching the final state during any matching process, it can be considered that the string input this time matches the regular expression. For example, in the above example where the regular expression is "abcc", the final state is S4, that is, "abcc". Generally speaking, there is only one initial state in the finite automaton, and there can be one or more final states, that is, starting from the same starting point, through different paths, it is possible to reach the end point of matching the regular expression.

[0031] The difference between an NFA and a DFA is that the state transition function in an NFA is a multi-valued function. That is, for an input character, the NFA may jump to two or more successor states, meaning the jump result of the NFA is uncertain. In contrast, the state transition function in a DFA is a single-valued function. For any input character, the successor state after the DFA jumps is unique, indicating that the jump result of the DFA is definite. Since the jump result of the NFA is uncertain, each jump in the NFA can be regarded as making a choice among all possible jumps, and the choice result may be incorrect. Therefore, a backtracking mechanism is introduced in the NFA. When the successor state after the jump cannot match the subsequent input characters, it is allowed to backtrack to the state before the jump and then select another jump path among all possible jumps. If all possible jumps have been tried and the subsequent input characters still cannot be matched, it can further backtrack to an earlier state and select other paths for matching until the correct result is matched or an error result is declared when all possibilities have been traversed and further matching is not possible. In the DFA, each match is a definite result. Therefore, the matching path in the DFA is single. When the DFA reaches a certain state and cannot continue to match, the failure of the current string match can be declared. Obviously, during the running process, since there is no need to backtrack various possibilities, the processing speed of the DFA engine is much faster than that of the NFA engine. However, when parsing regular expressions, the NFA engine can be directly compiled. To ensure that the result of each jump in the DFA engine is unique, in addition to compilation, the DFA engine also needs to traverse all possible results in the regular expression. Therefore, during the compilation process, the processing speed of the NFA engine is much faster than that of the DFA engine.

[0032] Whether it is an NFA engine or a DFA engine, during the compilation of regular expressions, they are compiled with one character corresponding to one state. Therefore, when the regular expression is more complex, that is, the number of characters in the regular expression is more, the number of states of the NFA or DFA constructed based on this regular expression is also more. Similarly, the number of state jumps during the matching process is also more. During the state jump process, a certain function library needs to be called for character set conversion to convert the input characters into byte format for processing. Therefore, each state jump consumes a certain amount of computing resources. Therefore, when the number of states is large and the number of state jump processes is large, the execution efficiency of the regular expression engine is lower, and the computing resources consumed during its execution are higher.

[0033] In response to this, the embodiments of this specification propose a method for constructing a regular expression engine, which can effectively reduce the number of states and the number of state jumps during the matching process, greatly improve the execution efficiency of the regular expression engine, and consume less computing resources.

[0034] As Figure 1 shown Figure 1 FIG. 5 is a flowchart of a method for constructing a regular expression engine according to an embodiment of the present specification, including the following steps:

[0035] Step S101, traverse each character of the regular expression, and mark consecutive ordinary characters in the regular expression as consecutive strings;

[0036] Step S103, construct an initial non-deterministic finite automaton based on the regular expression, and merge the jump relationships corresponding to the characters marked as consecutive strings into the same jump relationship.

[0037] In step S101, before constructing the finite automaton, all consecutive ordinary characters in the regular expression need to be marked.

[0038] Among them, ordinary characters refer to all characters in the regular expression except special characters. For example, any one of the uppercase letter characters A-Z, any one of the lowercase letter characters a-z, any one of the numeric characters 0-9, punctuation characters (such as ",", ";", "." etc.), and some special symbols. In addition, Chinese character characters also belong to ordinary characters. In contrast, the special characters in the regular expression mainly include "$", "(", ")", "*", "+", ".", "[", "?", "\", "^", "{", and "|", etc.

[0039] Among them, consecutive ordinary characters refer to several consecutive characters that are all ordinary characters, without any special characters in between. Consecutive ordinary characters can be marked with the same consecutive string label, and consecutive ordinary characters with the same consecutive string label can form a consecutive string to indicate the consecutive order relationship of these ordinary characters. In a regular expression, there may be multiple consecutive strings, and there is no direct relationship between each consecutive string. For example, the entire string "adcd" can be recognized as a consecutive string, while the entire string "ab|cd" cannot be recognized as a consecutive string. Instead, "ab" and "cd" need to be extracted separately and marked with different consecutive string labels to form two different consecutive strings.

[0040] In some embodiments, the continuous string recognized in a regular expression should be the longest continuous string that can be recognized, that is, the length of the continuous string should be maximally extended as much as possible, so as to minimize the number of states and state transitions and improve the execution efficiency. For example, if a regular expression is "abcde", the continuous string recognized by it should also be "abcde", rather than "abcd" or "bcde", so that the length of the continuous string is the maximum length that can be recognized.

[0041] In some embodiments, when consecutive ordinary characters in a regular expression are being matched, there is a direct connection relationship between each of these ordinary characters, and the characters within the continuous string do not form other operational relationships with the characters outside the continuous string. That is, the state transition relationship within the continuous string is determined and is not affected by other sub-expressions outside the continuous string in the regular expression. Therefore, when constructing an NFA, the transition relationships corresponding to the characters marked as the continuous string can be merged into the same transition relationship without affecting other transition relationships in the NFA, and the result of the constructed NFA will not be affected by the merging of the transition relationships.

[0042] It should be noted that in some embodiments, there are paired square brackets, that is, the symbols "[" and "]", in a regular expression, and when the character immediately preceding the characters on either side of the square brackets is not the escape character "\", it indicates that the entire substring enclosed by the square brackets in the regular expression is used to match any one of the characters in the square brackets. Therefore, even if consecutive ordinary characters can be recognized within the square brackets at this time, their meaning is different from that of the consecutive ordinary characters mentioned above, that is, the meaning of the continuous string. Specifically, the characters in the continuous string are in a sequential connection relationship, while the consecutive ordinary characters within the square brackets are in a selection relationship. For example, if the regular expression is "abcd", the result of matching this regular expression must also be "abcd", and no character can be missing, nor can it be in other sequential connections; while if the regular expression is "[abcd]", the result of matching this regular expression should be any one of the characters a, b, c, and d. Therefore, when the special characters "[" and "]" are recognized in a regular expression, there is no need to recognize consecutive ordinary characters between these two characters, or rather, the consecutive ordinary characters recognized between these two characters do not belong to the continuous string that needs to be merged in the embodiments of this specification.

[0043] In step S103, each character of the regular expression is traversed in parsing mode to construct an initial non-deterministic finite automaton, i.e., init-NFA. Moreover, during the construction of the init-NFA, when a consecutive string token in the regular expression is recognized, the jump relationships corresponding to each character marked as a consecutive string can be merged into the same jump relationship, that is, several state jump relationships in an ordered connection corresponding to the consecutive string are merged into the same state jump relationship. Since the jump relationships of the ordered consecutive states corresponding to the consecutive string are deterministic and there are no jump relationships between the jump relationships of the ordered consecutive states corresponding to the consecutive string and other states, the merged jump relationship will not affect the jump relationships between other states before and after the consecutive string. Instead, it avoids unnecessary state jump relationships within the consecutive string. Moreover, the finally generated init-NFA can also be used as a recognizer for matching each string with the regular expression. Therefore, when the execution mode of the regular expression engine is set to the NFA execution mode, the matching of the regular expression can be directly performed based on this init-NFA. Compared with the conventional NFA, since many unnecessary states and state jumps are reduced, the init-NFA of the embodiments of this specification has a faster execution efficiency.

[0044] Next, the embodiments of this specification illustrate the state diagrams of the traditional NFA and the init-NFA of the embodiments of this specification under the same regular expression through Figure 2 and Figure 3 . In Figure 2 and Figure 3 , the example shown has the regular expression "a*bcc".

[0045] In some embodiments, such as in Figure 2In the illustrated embodiment, the process of constructing a traditional NFA based on a regular expression can be to traverse the regular expression and construct the respective state transition relationships in sequence. First, when the ordinary character 'a' is recognized, a state transition relationship is constructed such that after inputting the character 'a' from the initial state S0, it jumps to state S1: S0×a→S1. Next, when the special character '*' is recognized, this special character indicates that its leading character, i.e., 'a', can appear zero or any number of times. Therefore, the transition relationship S0×a→S1 needs to be transformed into S0×ε→S1, S1×a→S1, and S1×ε→S2. Here, ε represents an empty input character, that is, a state transition can be achieved without inputting any character. It should be noted that since the state transition relationships in a DFA are deterministic, there is no state transition relationship with an input character of ε, that is, no input character, in a DFA. Therefore, the above transformation process can be understood as canceling the necessary path for the input character 'a' and constructing an empty path from the initial state S0 to state S2, that is, it is possible to jump from the initial state to state S1 without inputting any character, and it is also possible to continue jumping to state S2 without inputting any character to represent the possibility that the number of occurrences of 'a' in the target string is zero. Here, S2 is the final state of the current sub-expression "a*"; and a state transition relationship is also constructed such that after inputting the character 'a' from state S1, it can jump back to state S1 to represent the possibility that the number of occurrences of 'a' in the target string is any number of times. Next, when the ordinary character 'b' is recognized, a state transition relationship is constructed such that after inputting the character 'b' from state S2, it jumps to state S3: S2×b→S3. Next, when the ordinary character 'b' is recognized, a state transition relationship is constructed such that after inputting the character 'c' from state S3, it jumps to state S4: S3×c→S4. Next, when the ordinary character 'c' is recognized, a state transition relationship is constructed such that after inputting the character 'b' from state S4, it jumps to state S5: S4×b→S5. And no character is recognized after the character 'c', indicating that the regular expression ends here. Therefore, state S5 is the end state, and the circle corresponding to S5 in the state diagram becomes a concentric double circle representing the end state.

[0046] In some embodiments, the process of constructing an init-NFA based on a regular expression in the embodiments of this specification can be to first construct a complete traditional NFA based on the regular expression, and then merge the respective transition relationships corresponding to the characters marked as consecutive strings into the same transition relationship to generate an init-NFA. For example, in Figure 3 the illustrated embodiment, the process of constructing an init-NFA based on a regular expression can be to first, in accordance with Figure 2In the illustrated embodiment, a traditional NFA is constructed in the same manner, and the consecutive string "bcc" marked in step S101 is recognized. The state transition relationships corresponding to the respective characters "b", the first "c", and the second "c" of the consecutive string "bcc" are S2×b→S3, S3×c→S4, and S4×c→S5, respectively. Therefore, the above three state transition relationships can be combined into the same state transition relationship: S2×bcc→S3, and the final state of the combined sub-expression will inherit the other transition relationships and state attributes of the final state S5 of the sub-expressions before combination. For example, in Figure 2 In the illustrated embodiment, the final state of the combined sub-expression "bcc" is state S3, while the final state of the sub-expression "bcc" before combination is state S5. Therefore, state S3 will inherit the other transition relationships and state attributes of state S5, that is, state S3 becomes the end state in the init-NFA.

[0047] In some embodiments, the process of constructing the init-NFA based on the regular expression in the embodiments of this specification may also be to directly traverse the regular expression and construct each state transition relationship in sequence. When the characters in the recognized regular expression do not carry consecutive string markers, the method of constructing the state transition relationship based on the character is the same as the method of constructing the state transition relationship in the traditional NFA; when the characters in the recognized regular expression carry consecutive string markers, then continue to recognize each subsequent character until the next subsequent character recognized does not carry the consecutive string marker, that is, the entire consecutive string is recognized, and then construct the state transition relationship based on the consecutive string. The method of constructing the state transition relationship based on the consecutive string may be the same as the method of constructing the state transition relationship based on ordinary characters, and the difference is that the input character in the state transition relationship is changed from an ordinary character to a consecutive string. For example, in Figure 3 In the illustrated embodiment, for the regular expression "a*bcc", through step S101, it can be recognized that it includes a consecutive string "bcc". Therefore, the first character a and the second character * in the regular expression do not carry consecutive string markers, while the following three characters b, the first c, and the second c carry consecutive string markers. Therefore, first construct the state transition relationships of the first character a and the second character * in the traditional manner, and the specific construction process can be the same as the construction process of the state transition relationships of the first character a and the second character * in the traditional NFA in the Figure 2 illustrated embodiment, which will not be elaborated here. Then, it is recognized that the next character b carries a consecutive string marker, so it is necessary to continue to recognize the other characters following the character b until the recognized character does not carry the consecutive string marker, or until no other characters can be recognized, that is, until the last character of the entire regular expression is recognized. InFigure 2 In an embodiment, the recognized continuous string is "bcc". Therefore, a state transition relationship is constructed such that after inputting the continuous string bcc from state S2, it jumps to state S3: S2 × bcc → S3. Since no character is recognized after the continuous string "bcc", it indicates that the regular expression ends here. Therefore, state S3 is the end state, and the circle corresponding to S3 in the state diagram becomes a concentric double circle representing the end state.

[0048] For the regular expression "a*bcc", through Figure 2 and Figure 3 it can be known that there are a total of 6 states and 6 state transition relationships in the traditional NFA, while the init-NFA in the embodiments of this specification only has 4 states and 4 state transition relationships. Obviously, when there are more and longer continuous strings in the regular expression, compared with the traditional NFA, the init-NFA in the embodiments of this specification can reduce a large number of state quantities and state transition relationship quantities, and can significantly improve the execution efficiency.

[0049] It should be noted that the method of constructing the traditional NFA based on the regular expression is not only the construction method shown in the above embodiments. For example, for the regular expression "a*bcc", the traditional NFA constructed and generated is not only Figure 2 the one shown in the embodiment. Therefore, the method of constructing the init-NFA based on the regular expression in the embodiments of this specification is not only the construction method shown in the above embodiments. For example, for the regular expression "a*bcc", the init-NFA constructed and generated is not only Figure 3 the one shown in the embodiment. The method of constructing the init-NFA in the embodiments of this specification can also be other NFA construction methods, as long as the state transition relationships corresponding to each character marked as a continuous string are merged in this method, it can be regarded as the construction method of the init-NFA in the embodiments of this specification, and this specification does not limit this.

[0050] In some regular expression engines, the state transition relationships of the NFA can be stored in an NFA state transition relationship table. In some embodiments, the state transition relationships of the init-NFA in the embodiments of this specification can also be stored in the form of an NFA state transition relationship table.

[0051] In some embodiments, the jump characters of the jump relationship of the NFA can be divided into two categories. One category is single jump characters, such as 'a'. That is, a state of the NFA can jump to the next state after inputting the specified character 'a'. The other category is the jump character range, that is, multiple characters enclosed in square brackets, such as '[a-h]'. That is, a state of the NFA can jump to the next state after inputting any one of the characters in the jump character range, that is, any lowercase letter between 'a' and 'h'.

[0052] The traditional NFA state jump relationship table can be a two-dimensional matrix list. Among them, the rows are used to represent the various states of the NFA, the columns are used to represent the jump characters in the jump relationship, and the cells are used to represent the destination state that the state of the NFA in the row jumps to after inputting the jump character in the column. In particular, since the state of the NFA may jump to multiple different destination states after inputting the same character, therefore, multiple destination states can be stored in the cells of the NFA state jump relationship table. In the traditional NFA state jump relationship table, for the jump characters of the single jump character type, only the destination state needs to be written under the column where the character is located. For the jump characters of the jump character range type, the destination state needs to be written under all the columns where the characters included in the jump character range are located. Since whether it is the jump characters of the single jump character type or the jump characters of the jump character range, the corresponding destination state is written under the column where the single jump character is located, therefore, the storage structure of the traditional NFA state jump relationship table can be regarded as a single jump character-based storage structure.

[0053] In some embodiments, in order to include all possible input characters of the regular expression, the jump characters recorded in the NFA state jump relationship table generated by the regular expression engine need to include all possible input characters, that is, 256 columns need to be stored. In fact, the states in the NFA generally only involve a few jump characters or jump character ranges and do not involve all jump characters. Therefore, there is a lot of wasted storage space in the storage structure of the traditional NFA state jump relationship table.

[0054] In some embodiments, the storage structure of the NFA state transition relation table can be such that for each state of the NFA, all its state transition relations are stored separately for single transition characters and transition character ranges. For single transition characters of the single transition character type, all transition characters are recorded as either valid transition characters or invalid transition characters. For example, the traditional 256-column transition characters are merged into two columns, where one column represents valid transition characters and the other represents invalid transition characters. For transition characters of the transition character range type, all transition characters are also recorded as either valid transition characters or invalid transition characters. For example, the traditional 256-column transition characters are merged into three columns, where one column represents the valid transition character range, and the other two columns represent the two invalid transition character ranges on the left and right sides of the valid transition character range. Different from the storage structure in the traditional NFA state transition relation table, the transition characters in the transition character range do not need to be split into single transition characters for separate storage, and the transition characters within the same transition character range can be merged into one storage record. The storage records of the single transition character type and the storage records of the transition character range type can be merged into the same ordered array list for storage. Since in this state transition relation storage structure, single transition characters are all merged into the form of transition character ranges for storage, the storage structure of this NFA state transition relation table can be regarded as a transition character range-based storage structure. Compared with the traditional single transition character-based storage structure, the transition character range-based storage structure reduces many unnecessary recorded transition character columns. Therefore, this storage structure can effectively compress the storage space of the state transition relation table.

[0055] Particularly, in some embodiments, due to the existence of a backtracking mechanism in the NFA, when the input character is an invalid transition character or a character in the invalid transition character range, the NFA can also backtrack to the previous state to find other possible state transition relations and cannot directly declare the end of the matching process. Therefore, in the above-mentioned transition character range-based storage structure, invalid transition characters and invalid transition character ranges can also not be recorded.

[0056] Since the traditional single transition character-based state transition storage structure can directly match the target state according to the transition character, while the transition character range-based state transition storage structure needs to first find the transition character range where the transition character is located and then match the corresponding target state, the traditional single transition character-based storage structure has better matching performance than the transition character range-based storage structure.

[0057] Specifically, based on the principle of Markov chain, it can be known that for the state transition relationships in the NFA that are closer to the initial state, the probability of matching is higher. Therefore, in some embodiments, when storing the state transition relationships, for the state transition relationships that are closer to the initial state, that is, the state transition relationships of the states in the first few layers of the NFA, they can be stored in a single jump character-based storage structure; while for other state transition relationships that are farther from the initial state, they can be stored in a jump character interval-based storage structure. Herein, the level of a state in the NFA is used to represent the minimum number of jumps required to jump from the initial state to that state.

[0058] Specifically, in some embodiments, the state transition relationships of the states in the first three layers of the NFA can be stored in a single jump character-based storage structure; while the state transition relationships that do not belong to the first three layers are stored in a jump character interval-based storage structure.

[0059] In some regular expression engines, since the DFA has higher execution efficiency than the NFA, therefore, based on the NFA, a corresponding DFA is generated by determinizing the NFA, and then the DFA is used to match the string and the regular expression.

[0060] In some embodiments, the execution mode of the regular expression engine in this specification embodiment can be divided into an NFA execution mode and a DFA execution mode. When the execution mode is set to the NFA execution mode, the matching of the string and the regular expression can be directly performed based on the constructed init-NFA; while when the execution mode is set to the DFA execution mode, on the basis of the init-NFA, the init-NFA is converted into a DFA, and then the matching of the string and the regular expression is performed based on the converted DFA.

[0061] Specifically, in some embodiments, the manner of converting the init-NFA into a DFA can be as follows: first, convert the character set of each jump character in the init-NFA into a byte format to limit the maximum length of the state array. At this time, the states and state transition relationships generated by merging consecutive strings in the init-NFA will be split into multiple NFA states and state transition relationships. Then, based on the subset construction method, the init-NFA can be converted into a DFA. The subset construction method is a common method in the art for converting an NFA into a DFA, and this specification embodiment will not elaborate on it here.

[0062] During the conversion from NFA to DFA, a large part of the time is spent on querying whether the active NFA state set corresponding to the current jump character already exists. However, the jump edges of DFA states are often limited and there is a high probability of repetition. For example, in most cases, the jump characters of regular expressions only include the 64 common characters contained in BASE64, that is, the repetition rate is approximately 64 / 256 = 25%. Therefore, the active NFA state sets included in DFA states can be preprocessed. Since multiple consecutive jump characters have the same active NFA state set for jumping, only one Radix tree retrieval needs to be performed, greatly reducing the number of Radix tree retrievals, and thus shortening the execution time from NFA to DFA.

[0063] In some regular expression engines, the state jump relationship of DFA can be stored in a DFA state jump relationship table. In some embodiments, the converted DFA of the embodiments of this specification can also store the state jump relationship in the form of a DFA state jump relationship table.

[0064] In some embodiments, the jump characters of the jump relationship of DFA can be divided into two categories. One category is a single jump character, such as a, that is, a state of DFA can jump to the next state after inputting the specified character a. The other category is a jump character range, that is, multiple characters enclosed in square brackets, such as [a-h], that is, a state of DFA can jump to the next state after inputting any character in the jump character range, that is, any lowercase letter between a and h.

[0065] The traditional DFA state jump relationship table can be a two-dimensional matrix list. Among them, the rows are used to represent the various states of DFA, the columns are used to represent the jump characters in the jump relationship, and the cells are used to represent the destination state that the state of DFA in the row jumps to after inputting the jump character in the column. In particular, since a DFA state will only jump to the same destination state after inputting the same character, there is only one destination state stored in the cell of the DFA state jump relationship table. In the traditional DFA state jump relationship table, for the jump character of the single jump character type, only the destination state needs to be written under the column where the character is located, while for the jump character of the jump character range type, the destination state needs to be written under all the columns where the characters included in the jump character range are located. Since both the jump character of the single jump character type and the jump character of the jump character range are written with the corresponding destination state under the column where the single jump character is located, the storage structure of the traditional DFA state jump relationship table can be regarded as a single jump character-based storage structure.

[0066] In some embodiments, in order to encompass all possible input characters of a regular expression, the jump characters recorded in the DFA state transition relationship table generated by the regular expression engine need to include all possible input characters, that is, 256 columns need to be stored. In fact, the states in the DFA generally only involve a few jump characters or jump character ranges and do not involve all jump characters. Therefore, there is a lot of wasted storage space in the storage structure of the traditional NFA state transition relationship table.

[0067] In some embodiments, the storage structure of the DFA state transition relationship table can be such that for each state of the DFA, all its state transition relationships are stored separately for single jump characters and jump character ranges. For jump characters of the single jump character type, all jump characters are classified into two types: valid jump characters and invalid jump characters for recording. For example, the traditional 256 columns of jump characters are respectively merged into two columns, where one column represents valid jump characters and the other column represents invalid jump characters; for jump characters of the jump character range type, all jump characters are also classified into two types: valid jump characters and invalid jump characters for recording. For example, the traditional 256 columns of jump characters are respectively merged into three columns, where one column represents the valid jump character range, and the other two columns represent the two invalid jump character ranges on the left and right sides of the valid jump character range. Different from the storage structure in the traditional DFA state transition relationship table, the jump characters in the jump character range do not need to be split into single jump characters for separate storage, and the jump characters within the same jump character range can be merged into one storage record. The storage records of the single jump character type and the storage records of the jump character range type can be merged into the same ordered array list for storage. Since in this state transition relationship storage structure, single jump characters are all merged into the form of jump character ranges for storage, this storage structure of the DFA state transition relationship table can be regarded as a jump character range type of storage structure. Compared with the traditional single jump character type of storage structure, the jump character range type of storage structure reduces many jump character columns that are not necessary to record. Therefore, this storage structure can effectively compress the storage space of the state transition relationship table.

[0068] Particularly, in some embodiments, since the state transition relationship in the DFA is determined, when the input character is an invalid jump character or a character in the invalid jump character range, the DFA can directly announce the end of the matching process. Therefore, in the above-mentioned jump character range type of storage structure, it is necessary to record invalid jump characters and invalid jump character ranges.

[0069] Since the traditional single jump character-based state jump storage structure can directly match the target state according to the jump character, while the jump character interval-based state jump storage structure needs to first find the jump character interval where the jump character is located and then match the corresponding target state, the traditional single jump character-based storage structure has better matching performance than the jump character interval-based storage structure.

[0070] Specifically, based on the principle of Markov chain, it can be known that the closer the state jump relationship in the DFA is to the initial state, the higher the probability of its matching. Therefore, in some embodiments, when storing the state jump relationship, for the state jump relationship relatively close to the initial state, that is, the state jump relationship of the states in the first few layers of the DFA, it can be stored in a single jump character-based storage structure; while for other state jump relationships that are far from the initial state, they can be stored in a jump character interval-based storage structure. Wherein, the level of the state in the DFA is used to represent the minimum number of jumps required to jump from the initial state to this state.

[0071] Specifically, in some embodiments, the state jump relationships of the states in the first three layers of the DFA can be stored in a single jump character-based storage structure; while the state jump relationships that do not belong to the first three layers are stored in a jump character interval-based storage structure.

[0072] Next, the embodiments of this specification will further elaborate on the construction method of the regular expression engine of this specification through a specific embodiment.

[0073] As Figure 4 shown, Figure 4 is a schematic flowchart of a method for constructing a regular expression engine according to an embodiment of this specification.

[0074] First, in step S401, when receiving a command for a regular expression and constructing a regular expression engine based on this regular expression, the regular expression will first be converted into an initial non-deterministic finite automaton, that is, init-NFA. Specifically, before constructing the init-NFA, the regular expression will be traversed first, and the consecutive ordinary characters in the regular expression will be marked as consecutive strings, and then the characters of the regular expression will be traversed in the parsing mode to construct the init-NFA. Among them, when the traversed character carries a consecutive string mark, the state jump relationships between multiple subsequent characters carrying the same consecutive string mark will be merged, so that one consecutive string corresponds to only one state jump relationship.

[0075] In step S402, after constructing the init-NFA, it is necessary to determine whether the current execution mode is the DFA mode, and then determine whether it is necessary to convert the init-NFA into a DFA. If the current execution mode is the NFA mode, that is, not the DFA mode, then directly use the init-NFA as the result regular expression engine, and use the init-NFA to perform the matching of the regular expression and the string text, that is, end the entire process. If the current execution mode is the DFA mode, then it is necessary to convert the init-NFA into the corresponding DFA through subsequent steps to use the DFA to perform the matching of the regular expression and the string text, that is, enter step S403.

[0076] In step S403, before converting the init-NFA into the corresponding DFA, it is necessary to first convert the character set of each jump character in the init-NFA into the byte format to limit the maximum length of the state array. At this time, the state and state jump relationships generated by the merger of consecutive strings in the init-NFA will be split into multiple NFA states and state jump relationships. The state jump relationships between each of the split states only jump through a single jump character, so that the state jump relationships between each of the split states can be stored in the form of a state jump relationship table.

[0077] In step S404, when storing the NFA state jump relationships of each state of the init-NFA in the state jump relationship table, it is possible to first determine whether the init-NFA state before the NFA state jump relationship to be stored jumps is the first three layers of the init-NFA, that is, the minimum number of jumps required to jump from the initial state to the init-NFA state is no more than three. In step S4041, if the current init-NFA state is a state in the first three layers of the init-NFA, it means that the current NFA jump relationship has a relatively high probability of being matched during the process of matching the regular expression. Therefore, a method with a faster processing speed can be used, that is, a single jump character type storage structure is used to store the current NFA state jump relationship. In step S4042, if the current init-NFA state is not a state in the first three layers of the init-NFA, it means that the current NFA jump relationship has a relatively low probability of being matched during the process of matching the regular expression. Therefore, a method that occupies less storage space can be used, that is, a jump character interval type storage structure is used to store the current NFA state jump relationship.

[0078] In step S405, after the NFA state transition relationships of each state in the init-NFA are all stored, the NFA state transition relationships stored in all single jump character-based storage structures and the NFA state transition relationships stored in all jump character interval-based storage structures can be merged into the same ordered array list, and this ordered array list is the state transition relationship table of the init-NFA.

[0079] In step S406, when converting the init-NFA into the corresponding DFA, it can first be determined whether the current DFA state is in the first three layers of the DFA, and the layer where the current DFA state is located can be determined according to the layer where the NFA state corresponding to the current DFA state is located in the init-NFA before conversion. In step S4061, if the current DFA state is a state in the first three layers of the DFA, preprocess the active NFA state set included in the current DFA state, where the single jump characters of the NFA state corresponding to the current DFA state have the same jump active NFA state set; in step S4062, if the current DFA state is not a state in the first three layers of the DFA, preprocess the active NFA state set included in the current DFA state, where the multiple consecutive jump characters of the NFA state corresponding to the current DFA state have the same jump active NFA state set.

[0080] In step S407, after preprocessing the active NFA state sets included in each DFA state, it is necessary to determine whether the current DFA contains a remaining active NFA state set. If the current DFA still contains a remaining active NFA state set, it indicates that there is still room for optimization of the current DFA, and the current DFA can be further optimized, that is, enter step S408. In step S408, when optimizing the DFA, it is possible to first use a Radix tree to search for a DFA state that already contains an active NFA state set identical to the remaining active NFA state set; if it exists, the remaining active NFA state set can be incorporated into this DFA state, and it is determined whether there are other remaining NFA state sets, that is, return to step S407; if there is no DFA state identical to the remaining active NFA state set, then in step S409, a new DFA state can be created, and the DFA state information can be added to the Radix tree. Then, in step S410, the original DFA state transition relationship can be modified so that the original DFA state relationship changes from the DFA state before the original jump to the DFA state after the jump to the DFA state before the jump to the new DFA state, and then from the new DFA state to the DFA state after the jump. After adding the new DFA state, it is possible to continue to determine whether there are still remaining active NFA state sets, that is, return to step S407. In step S407, if there are no remaining active NFA state sets in the current DFA, it indicates that the DFA optimization is complete, and the DFA can be used as the resulting regular expression engine to perform regular expression and string text matching using this DFA, and the entire process ends.

[0081] Corresponding to the embodiment of the method for constructing a regular expression engine described above, the embodiment of the present specification also provides a device for constructing a regular expression engine.

[0082] As Figure 5 shown, Figure 5 is a schematic structural diagram of a device for constructing a regular expression engine shown in an embodiment of the present specification, including the following modules:

[0083] Character recognition module 510: used to traverse each character of the regular expression and mark consecutive ordinary characters in the regular expression as consecutive strings;

[0084] Engine construction module 520: used to construct an initial non-deterministic finite automaton based on the regular expression and merge the jump relationships corresponding to the characters marked as consecutive strings into the same jump relationship.

[0085] The implementation processes of the functions and roles of each unit in the above device are specifically described in detail in the implementation processes of the corresponding steps in the above method, and will not be elaborated here.

[0086] For the apparatus embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to the descriptions in the method embodiments. The apparatus embodiments described above are merely illustrative. The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solutions of the embodiments of this specification. A person of ordinary skill in the art can understand and implement them without creative efforts.

[0087] The embodiments of this specification also provide a computer device, which at least includes a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, it implements the method described in any of the foregoing embodiments.

[0088] Figure 6 FIG. shows a more specific schematic diagram of the hardware structure of a computing device provided by the embodiments of this specification. The device may include: a processor 601, a memory 602, an input / output interface 603, a communication interface 604, and a bus 605. Among them, the processor 601, the memory 602, the input / output interface 603, and the communication interface 604 are communicatively connected to each other inside the device through the bus 605.

[0089] The processor 601 can be implemented in ways such as a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided by the embodiments of this specification. The processor 601 may further include a graphics card, and the graphics card may be an Nvidia titan X graphics card or a 1080Ti graphics card, etc.

[0090] The memory 602 can be implemented in forms such as a ROM (Read Only Memory), a RAM (Random Access Memory), a static storage device, a dynamic storage device, etc. The memory 602 can store an operating system and other application programs. When implementing the technical solutions provided by the embodiments of this specification through software or firmware, the relevant program codes are stored in the memory 602 and called and executed by the processor 601.

[0091] The input / output interface 603 is used to connect to the input / output module to achieve information input and output. The input / output module can be configured as a component in the device (not shown in the figure), or can be externally connected to the device to provide corresponding functions. The input devices can include a keyboard, a mouse, a touch screen, a microphone, various sensors, etc., and the output devices can include a display, a speaker, a vibrator, an indicator light, etc.

[0092] The communication interface 604 is used to connect to the communication module (not shown in the figure) to achieve communication interaction between this device and other devices. The communication module can achieve communication through a wired manner (such as USB, network cable, etc.), or can also achieve communication through a wireless manner (such as mobile network, WIFI, Bluetooth, etc.).

[0093] The bus 605 includes a path to transmit information between various components of the device (such as the processor 601, the memory 602, the input / output interface 603, and the communication interface 604).

[0094] It should be noted that although the above device only shows the processor 601, the memory 602, the input / output interface 603, the communication interface 604, and the bus 605, in the specific implementation process, the device may also include other components necessary for normal operation. In addition, those skilled in the art can understand that the above device may also only include the components necessary to implement the solution of the embodiments of this specification, and does not necessarily include all the components shown in the figure.

[0095] The embodiments of this specification also provide a computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processor, it implements the method described in any one of the foregoing embodiments.

[0096] Computer-readable media include permanent and non-permanent, removable and non-removable media and can store information by any method or technology. The information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette tapes, magnetic disk storage or other magnetic storage devices, or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media do not include transitory computer-readable media, such as modulated data signals and carrier waves.

[0097] As can be understood from the description of the above embodiments, those skilled in the art can clearly understand that the embodiments of this specification can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solutions of the embodiments of this specification, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments of this specification.

[0098] The systems, devices, modules or units illustrated in the above embodiments can be specifically implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer, and the specific form of the computer can be a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email transceiver, a game console, a tablet computer, a wearable device, or a combination of any several of these devices.

[0099] Each embodiment in this specification is described in a progressive manner. For the same or similar parts among the embodiments, reference can be made to each other. Each embodiment focuses on the differences from other embodiments. In particular, for the device embodiments, since they are basically similar to the method embodiments, the description is relatively simple. For the relevant parts, reference can be made to the partial description of the method embodiments. The device embodiments described above are only illustrative. The modules described as separate components may or may not be physically separated. When implementing the solutions of the embodiments of this specification, the functions of each module can be implemented in the same or multiple software and / or hardware. It is also possible to select some or all of the modules according to actual needs to achieve the purpose of the solutions of this embodiment. Those of ordinary skill in the art can understand and implement it without creative efforts.

[0100] The above are only the specific implementation manners of the embodiments of this specification. It should be noted that for those of ordinary skill in the art in this technical field, without departing from the principle of the embodiments of this specification, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the embodiments of this specification.

Claims

1. A construction method of a regular expression engine, the method comprising: Traverse each character of the regular expression, and mark consecutive ordinary characters in the regular expression as consecutive strings; wherein, if there are paired symbols for representing brackets in the regular expression, the consecutive ordinary characters include: in the regular expression, consecutive ordinary characters other than the characters between the paired symbols; Construct an initial non-deterministic finite automaton (init-NFA) based on the regular expression, wherein the NFA state transition relationships corresponding to the characters marked as the consecutive strings are merged into the same NFA state transition relationship; If the current execution mode is the NFA mode, use the init-NFA as the regular expression engine; If the current execution mode is the DFA mode, convert the character sets of the jump characters of the init-NFA into byte format, so as to split the states and state transition relationships generated by merging consecutive strings in the init-NFA into multiple NFA states and state transition relationships, and the state transition relationships between each pair of split NFA states are jumped through a single jump character; Store and record the NFA state transition relationships of each state of the init-NFA in a state transition relationship table; wherein, for each currently to-be-stored NFA state transition relationship, if the minimum number of jumps required to jump from the initial state to this current NFA state is not greater than a preset number threshold, use a single jump character-based storage structure for storage; otherwise, use a jump character range-based storage structure for storage.

2. The method according to claim 1, the method further comprising: Record the NFA state transition relationships of the initial non-deterministic finite automaton in an NFA state transition relationship table.

3. The step of recording the NFA state transition relationships of the initial non-deterministic finite automaton in an NFA state transition relationship table according to claim 2 comprises: When the jump character of the NFA state transition relationship is a single jump character, record the valid jump character and the invalid jump character of the NFA state transition relationship in the NFA state transition relationship table; When the jump character of the NFA state transition relationship is a jump character range, record the valid jump character range and the invalid jump character range of the NFA state transition relationship in the NFA state transition relationship table.

4. The method according to claim 2, the method further comprising: When the minimum number of jumps required for the initial state of the initial non-deterministic finite automaton to jump to the state before the jump in the NFA state transition relationship is not greater than the preset number threshold, record the state transition relationship in the NFA state transition relationship table in the form of a single jump character array.

5. The preset number threshold according to claim 4 is 3.

6. The method according to claim 1, the method further comprising: When the execution mode is set to the DFA execution mode, convert the initial non-deterministic finite automaton into a deterministic finite automaton.

7. The method according to claim 6, wherein the method further comprises: recording the DFA state transition relationship of the deterministic finite automaton in a DFA state transition relationship table.

8. The method according to claim 7, wherein the step of recording the DFA state transition relationship of the deterministic finite automaton in a DFA state transition relationship table comprises: when the transition character of the DFA state transition relationship is a single transition character, recording the valid transition character and the invalid transition character of the DFA state transition relationship in the DFA state transition relationship table; when the transition character of the DFA state transition relationship is a transition character range, recording the valid transition character range and the invalid transition character range of the DFA state transition relationship in the DFA state transition relationship table.

9. The method according to claim 1, wherein the preset number threshold is 3.

10. An apparatus for constructing a regular expression engine, the apparatus comprising: a character recognition module, configured to traverse each character of the regular expression and mark consecutive ordinary characters in the regular expression as a consecutive string; wherein, if there are paired symbols for representing square brackets in the regular expression, the consecutive ordinary characters include: in the regular expression, consecutive ordinary characters other than the characters between the paired symbols; an engine construction module, configured to construct an initial non-deterministic finite automaton init-NFA based on the regular expression, wherein the NFA state transition relationships corresponding to the characters marked as the consecutive string are merged into the same NFA state transition relationship; wherein, if the current execution mode is the NFA mode, using the init-NFA as the regular expression engine; if the current execution mode is the DFA mode, converting the character set of each transition character of the init-NFA into a byte format, so as to split the state and the state transition relationship generated by merging consecutive strings in the init-NFA into multiple NFA states and state transition relationships, and the state transition relationship between each pair of the split NFA states is jumped through a single transition character; storing and recording the NFA state transition relationships of each state of the init-NFA in a state transition relationship table; wherein, for each current NFA state transition relationship to be stored, if the minimum number of jumps required to jump from the initial state to the current NFA state is not greater than the preset number threshold, storing it using a single transition character type storage structure; otherwise, storing it using a transition character range type storage structure.

11. A computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processor, the method according to any one of claims 1-9 is implemented.

12. A computer device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, and when the processor executes the computer program, the method according to any one of claims 1-9 is implemented.

Citation Information

Patent Citations

  • Construction method and device of regular expression NFA

    CN111159496A

  • DFA space compression method and device

    CN111262589A