Bidirectional DFA scanning matching method, device, equipment and medium
By introducing inverse DFA in DFA and scanning and matching strings from both front and reverse directions, the existing DFA's slow scanning speed and low matching performance are solved, and more efficient string matching is achieved.
Patent Information
- Application Number
- CN202510654293.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-21
- Publication Date
- 2025-06-20
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
During the string scan matching process, existing DFAs have slower scanning speed and lower matching performance due to one-way serial matching.
By determining the current state and next hop state of the target character processed with all states of the forward DFA, when the asymmetric state of the target character is transferred, the current state and next hop state of the target character are replaced to obtain the reverse DFA corresponding to the forward DFA, and the string is scanned and matched from both forward and reverse directions through the forward DFA and the reverse DFA.
The scanning speed of DFA is improved, thereby improving the scanning performance and matching performance of DFA.
Smart Images

Figure CN120179874A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of data processing. Specifically, it relates to a bidirectional DFA scanning and matching method, device, equipment, and medium. Background Technique
[0002] The core principle of regular expressions is based on the idea of pattern matching. By defining a pattern, the parts of a string that conform to the pattern can be matched. This pattern can describe the structure, format, or characteristics of the string, thereby achieving precise positioning and extraction of the string.
[0003] Currently, regular expression rules can be equivalently compiled and transformed into a Deterministic Finite Automaton (DFA) for string scanning. When using the original DFA to scan a string, the process is a serial scan and match from left to right.
[0004] However, due to its one-way serial matching, the scanning speed is slow, resulting in low scanning performance, which also means low matching performance of the DFA. Summary of the Invention
[0005] In view of this, the purpose of this application is to provide a bidirectional DFA scanning and matching method, device, equipment, and medium. By determining that when the current state and the next-hop state of the target character processed by all states of the preset forward DFA are in an asymmetric state transition, the current state and the next-hop state of the target character are replaced to obtain the reverse DFA corresponding to the forward DFA, and the forward DFA and the reverse DFA are used to scan and match the string from both positive and negative directions, that is, the forward DFA scans and matches the positive string from left to right, and the reverse DFA scans and matches the reverse string from right to left. Thus, the scanning speed of the DFA is improved, and the scanning performance and matching performance of the DFA are thereby improved.
[0006] In a first aspect, an embodiment of this application provides a bidirectional DFA scanning and matching method, and the method includes: Based on all states of the preset forward DFA, process a target number of target characters respectively, and determine whether the current state and the next-hop state corresponding to the target character are in a symmetric state transition of the target character; If the current state and the next-hop state corresponding to the target character are not in a symmetric state transition of the target character, then based on the preset target state, replace the current state and the next-hop state of the target character to obtain the reverse DFA corresponding to the forward DFA, and determine the reverse string corresponding to the preset forward string; Scanning the forward string through the forward DFA and scanning the reverse string through the reverse DFA, and obtaining an overall scanning result during two-way scanning according to the respective scanning results; wherein, the reverse string is the reverse of the forward string.
[0007] In a possible implementation manner, determining whether the current state and the next-hop state corresponding to the target character are symmetric state transitions of the target character includes: In response to the forward DFA, when it is established that the forward DFA jumps from the current state to the next-hop state when processing the target character, determining whether it is established that the forward DFA jumps from the next-hop state to the current state when processing the target character; If it is established that the forward DFA jumps from the next-hop state to the current state when processing the target character, the current state and the next-hop state are symmetric state transitions of the target character.
[0008] In a possible implementation manner, scanning the forward string through the forward DFA and scanning the reverse string through the reverse DFA includes: Scanning and matching the forward string through the forward DFA in the order from left to right, obtaining a corresponding forward scanning result, recording the position of the first character where the currently scanned character is located, and incrementing the position of the first character every time a character is processed subsequently; Scanning and matching the reverse string through the reverse DFA in the order from right to left, obtaining a corresponding reverse scanning result, recording the position of the second character where the currently scanned character is located, and decrementing the position of the second character every time a character is processed subsequently. In a possible implementation manner, obtaining the overall scanning result during two-way scanning according to the respective scanning results includes: When the position of the first character is equal to the position of the second character, determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are reverse; In response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are symmetric states, the forward DFA and the reverse DFA stop scanning and matching, and merge the forward scanning result of the forward DFA and the reverse scanning result of the reverse DFA in the forward order at this time to obtain a merged overall scanning result.
[0009] In a possible implementation manner, the two-way DFA scanning and matching method further includes: Determine the first hit rule flag for determining whether a preset target rule is hit during the process of the forward DFA processing the forward string, and the second hit rule flag to be determined during the process of the reverse DFA processing the reverse string; In response to the current forward state of the forward DFA and the current reverse state of the reverse DFA being in a symmetric state, based on the first hit rule flag and the second hit rule flag, obtain a target hit rule flag for finally determining whether the forward string hits the target rule; Based on the target hit rule flag, determine whether the forward string hits the target rule.
[0010] In a possible implementation manner, the bidirectional DFA scanning and matching method further includes: In response to the transfer state accessed during the process of the forward DFA scanning the forward string being a pre-determined acceptable state, assign a value to the first hit rule flag; In response to the transfer state accessed during the process of the reverse DFA scanning the reverse string being the acceptable state, assign a value to the second hit rule flag; In response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are in a symmetric state, perform an OR operation on the first hit rule flag and the second hit rule flag to obtain the target hit rule flag.
[0011] In a possible implementation manner, the bidirectional DFA scanning and matching method further includes: In response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are in an asymmetric state, the reverse DFA stops scanning and matching; The forward DFA continues to scan and match the forward string to the right until the forward string is scanned completely, and the corresponding scan result is used as the overall scan result, and the first hit rule flag is determined as the target hit rule flag.
[0012] In a second aspect, an embodiment of the present application further provides a bidirectional DFA scanning and matching device, and the device includes: A first determination module, configured to process a target number of target characters respectively based on all states of a preset forward DFA, and determine whether the current state and the next-hop state corresponding to the target character are symmetric state transitions of the target character; A replacement module, configured to, if the current state and the next-hop state corresponding to the target character are not symmetric state transitions of the target character, replace the current state and the next-hop state of the target character based on a preset target state, obtain a reverse DFA corresponding to the forward DFA, and determine a reverse string corresponding to a preset forward string; A scanning module, configured to scan the forward string through the forward DFA, and scan the reverse string through the reverse DFA, and obtain an overall scanning result during two-way scanning according to the respective scanning results; wherein, the reverse string is the reverse string of the forward string.
[0013] In a possible implementation manner, the first determination module is specifically configured to: In response to, for the forward DFA, when it is established that the forward DFA jumps from the current state to the next-hop state when processing the target character, determine whether it is established that the forward DFA jumps from the next-hop state to the current state when processing the target character; If it is established that the forward DFA jumps from the next-hop state to the current state when processing the target character, the current state and the next-hop state are symmetric state transitions of the target character.
[0014] In a possible implementation manner, the scanning module is specifically configured to: Scan and match the forward string through the forward DFA in the order from left to right, obtain a corresponding forward scanning result, record the position of the first character where the currently scanned character is located, and increment the position of the first character every time a subsequent character is processed; Scan and match the reverse string through the reverse DFA in the order from right to left, obtain a corresponding reverse scanning result, record the position of the second character where the currently scanned character is located, and decrement the position of the second character every time a subsequent character is processed. In a possible implementation manner, the scanning module is specifically configured to: When the position of the first character is equal to the position of the second character, determine that the current forward state of the forward DFA and the current reverse state of the reverse DFA are reverse; In response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are symmetric states, the forward DFA and the reverse DFA stop scanning and matching, and merge the forward scanning result of the forward DFA and the reverse scanning result of the reverse DFA in the forward order at this time to obtain an overall scanning result after merging.
[0015] In a possible implementation manner, the two-way DFA scanning and matching device includes: A second determination module, configured to determine a first hit rule flag indicating whether a preset target rule is hit during the process of the forward DFA processing the forward string, and a second hit rule flag to be determined during the process of the reverse DFA processing the reverse string; An acquisition module, configured to, in response to the current forward state of the forward DFA and the current reverse state of the reverse DFA being in a symmetric state, obtain a target hit rule flag for finally determining whether the forward string hits the target rule based on the first hit rule flag and the second hit rule flag; A third determination module, configured to determine whether the forward string hits the target rule based on the target hit rule flag.
[0016] In a possible implementation manner, the bidirectional DFA scanning and matching device includes: A first assignment module, configured to, in response to the transfer state accessed during the process of the forward DFA scanning the forward string being a pre-determined acceptable state, assign a value to the first hit rule flag; A second assignment module, configured to, in response to the transfer state accessed during the process of the reverse DFA scanning the reverse string being the acceptable state, assign a value to the second hit rule flag; A calculation module, configured to, in response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are in a symmetric state, perform an OR operation on the first hit rule flag and the second hit rule flag to obtain the target hit rule flag. In a possible implementation manner, the bidirectional DFA scanning and matching device includes: A fourth determination module, configured to, in response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are in an asymmetric state, stop the reverse DFA from scanning and matching; A fifth determination module, configured to continue to scan and match the forward string to the right through the forward DFA until the forward string is scanned completely, obtain a corresponding scan result as an overall scan result, and determine the first hit rule flag as the target hit rule flag.
[0017] In a third aspect, an embodiment of the present application provides an electronic device, including: a processor, a storage medium, and a bus. The storage medium stores machine-readable instructions executable by the processor. When the electronic device runs, the processor communicates with the storage medium through the bus, and the processor executes the machine-readable instructions to perform the steps of the bidirectional DFA scanning and matching method according to any one of the first aspects.
[0018] Fourthly, an embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is run by a processor, it executes the steps of the bidirectional DFA scanning and matching method according to any one of the first aspect.
[0019] A bidirectional DFA scanning and matching method, device, equipment and medium provided by an embodiment of the present application process a target number of target characters respectively based on all states of a preset forward DFA, and determine whether the current state and the next-hop state corresponding to the target character are symmetric state transitions of the target character. If the current state and the next-hop state corresponding to the target character are not symmetric state transitions of the target character, then replace the current state and the next-hop state of the target character based on a preset target state to obtain a reverse DFA corresponding to the forward DFA, and determine a reverse string corresponding to a preset forward string. Scan the forward string through the forward DFA, and scan the reverse string through the reverse DFA, and obtain an overall scanning result during bidirectional scanning according to the respective scanning results. In the present application, when it is determined that the current state and the next-hop state of the target character processed by using all states of the forward DFA are not symmetric state transitions, the current state and the next-hop state of the target character are replaced to obtain a reverse DFA corresponding to the forward DFA, and the forward DFA and the reverse DFA are used to perform scanning and matching on the string in both the forward and reverse directions, that is, the forward DFA scans and matches the forward string from left to right, and the reverse DFA scans and matches the reverse string from right to left. Thus, the scanning speed of the DFA is improved, and thereby the scanning performance and matching performance of the DFA are improved.
[0020] To make the above objects, features, and advantages of the present application more obvious and understandable, the following specifically enumerates preferred embodiments and, in conjunction with the accompanying drawings, provides a detailed description as follows. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] To more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings required to be used in the embodiments. It should be understood that the following drawings only show some embodiments of the present application, and therefore should not be regarded as limiting the scope. For those of ordinary skill in the art, other related drawings can be obtained based on these drawings without creative efforts.
[0022] Figure 1 is a flowchart of the bidirectional DFA scanning and matching method provided by an embodiment of the present application; Figure 2 is a schematic diagram of a forward DFA; Figure 3 is a schematic diagram of a reverse DFA; Figure 4It is a schematic structural diagram of a bidirectional DFA scanning and matching device provided according to an embodiment of the present application; Figure 5 It is a schematic structural diagram of an electronic device provided according to an embodiment of the present application. Detailed implementation manners
[0023] To make the objectives, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application. It should be understood that the accompanying drawings in the present application are only for the purposes of illustration and description, and are not used to limit the protection scope of the present application. In addition, it should be understood that the schematic drawings are not drawn to scale. The flowcharts used in the present application illustrate the operations implemented according to some embodiments of the present application. It should be understood that the operations in the flowchart may not be implemented in sequence, and steps without logical context relationships may be reversed or implemented simultaneously. In addition, those skilled in the art may add one or more other operations to the flowchart or remove one or more operations from the flowchart under the guidance of the content of the present application.
[0024] In addition, the described embodiments are only some embodiments of the present application, rather than all embodiments. The components of the embodiments of the present application usually described and illustrated in the accompanying drawings here may be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the present application claimed, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative efforts belong to the protection scope of the present application.
[0025] It should be noted that the term "including" will be used in the embodiments of the present application to indicate the existence of the features stated hereinafter, but does not exclude the addition of other features.
[0026] Considering that the core principle of regular expressions is based on the idea of pattern matching, by defining a pattern to match the part of the string that conforms to the pattern. This pattern can describe the structure, format or features of the string, so as to achieve precise positioning and extraction of the string.
[0027] Currently, regular expression rules can be equivalently compiled and transformed into a deterministic finite automaton (DFA) for string scanning. When using the original DFA to scan a string, the process is a serial scan and match from left to right. However, due to its one-way serial matching, the scanning speed is slow, resulting in low scanning performance, which also means low matching performance of the DFA.
[0028] In view of this problem, the present application provides a bidirectional DFA scanning and matching method, apparatus, device, and medium. By determining that when the current state and the next-hop state of the target character processed by all states of the forward DFA are asymmetric state transitions, the current state and the next-hop state of the target character are replaced to obtain the reverse DFA corresponding to the forward DFA. Then, the forward DFA and the reverse DFA are used to scan and match the string from both the forward and reverse directions, that is, the forward DFA scans and matches the forward string from left to right, and the reverse DFA scans and matches the reverse string from right to left. Thus, the scanning speed of the DFA is improved, and the scanning performance and matching performance of the DFA are thereby improved.
[0029] Figure 1 is a flowchart of the bidirectional DFA scanning and matching method provided by an embodiment of the present application. As Figure 1 shown, the bidirectional DFA scanning and matching method of the embodiment of the present application may specifically include: S101. Process a target number of target characters based on all states of a preset forward DFA, and determine whether the current state and the next-hop state corresponding to the target character are symmetric state transitions of the target character.
[0030] S102. If the current state and the next-hop state corresponding to the target character are not symmetric state transitions of the target character, replace the current state and the next-hop state of the target character based on a preset target state to obtain the reverse DFA corresponding to the forward DFA, and determine the reverse string corresponding to the preset forward string.
[0031] S103. Scan the forward string through the forward DFA and scan the reverse string through the reverse DFA, and obtain the overall scanning result during bidirectional scanning according to the respective scanning results.
[0032] In the above bidirectional DFA scanning and matching method, by determining that when the current state and the next-hop state of the target character processed by all states of the forward DFA are asymmetric state transitions, the current state and the next-hop state of the target character are replaced to obtain the reverse DFA corresponding to the forward DFA. Then, the forward DFA and the reverse DFA are used to scan and match the string from both the forward and reverse directions, that is, the forward DFA scans and matches the forward string from left to right, and the reverse DFA scans and matches the reverse string from right to left. Thus, the scanning speed of the DFA is improved, and the scanning performance and matching performance of the DFA are thereby improved.
[0033] The following describes the above exemplary steps of the embodiment of the present application in combination with specific examples: S101. Process the target number of target characters respectively based on all states of a preset forward DFA, and determine whether the current state and the next-hop state corresponding to the target characters are symmetric state transitions of the target characters.
[0034] Those skilled in the art can understand that regular expression rules can be equivalently compiled and transformed into a deterministic finite automaton (DFA) for string scanning. For example, as Figure 2 shown, it is the original DFA obtained by equivalently compiling and transforming regular expression rules, that is, an example of the forward DFA of this application. This application describes it by taking this as an example; in addition, determine the string T = "abcbcbc", that is, an example of the forward string of this application. This application describes it by taking this as an example.
[0035] In the embodiments of this application, the forward DFA represents the original DFA. All states of the forward DFA can include 0, 1,..., N. As Figure 2 shown, all states of the forward DFA at this time are 0, 1, 2. The target number of target characters are multiple characters preprocessed by the forward DFA. For example, 256 ASCII characters. This application describes it by taking this as an example, but it does not constitute a limitation thereto; the target character is the character being processed by the forward DFA among the 256 ASCII characters. Using all states of the forward DFA, process the 256 ASCII characters respectively to find symmetric state transitions. The forward DFA processing is to scan a target character (input_char) among the 256 ASCII characters, and determine whether the current state Scur and the next-hop state Snext corresponding to the target character are symmetric state transitions of the target character for subsequent processing.
[0036] It should be noted that when determining whether the current state and the next-hop state corresponding to the target character are symmetric state transitions of the target character, in response to the forward DFA, when it is established that the forward DFA jumps from the current state Scur to the next-hop state Snext when processing the target character, it is judged whether it is also established that the forward DFA jumps from the next-hop state Snext to the current state Scur when processing the target character; if so, the current state Scur and the next-hop state Snext are symmetric state transitions of the target character input_char.
[0037] Specifically, assume that the triple (Scur, input_char, Snext) represents that after processing a single target character input_char among 256 ASCII characters by the forward DFA, the current state Scur jumps to the next state Snext. For the forward DFA, if (Scur, input_char, Snext) holds and (Snext, input_char, Scur) also holds, then Scur and Snext are called symmetric state transitions with respect to the single target character input_char of the input. For example, under the forward DFA as shown in Figure 2 (0, a, 0) and (0, a, 0), (0, b, 0) and (0, b, 0), (0, c, 1) and (1, c, 0), (1, b, 2) and (2, b, 1), (2, c, 0) and (2, c, 0).
[0038] S102, if the current state and the next state corresponding to the target character are not symmetric state transitions of the target character, then replace the current state and the next state of the target character based on a preset target state to obtain the reverse DFA corresponding to the forward DFA, and determine the reverse string corresponding to the preset forward string.
[0039] In the embodiments of the present application, the forward string is the preset forward string (denoted by T). The present application describes it by taking the forward string T = "abcbcbc" as an example; the target state is used to replace the current state and the next state of the character, and can be considered as a negative number. When the DFA scans to this target state, the scanning will end. When it is determined in step S101 that the current state Scur and the next state Snext corresponding to the target character are not symmetric state transitions of the target character, then replace the current state and the next state of the target character with the target state N, that is, both the current state and the next state of the target character are replaced with N, to obtain the reverse DFA corresponding to the forward DFA (also namely the reverse DFA', for the convenience of description in the present application, it is named the reverse DFA), and determine the reverse string T' = "cbcbcba" corresponding to the preset forward string T = "abcbcbc" for subsequent processing. For example, as shown in Figure 3 shown, it is the Figure 2 reverse DFA corresponding to the forward DFA shown.
[0040] It can be supplemented that when the current state and the next state corresponding to the target character are symmetric state transitions of the target character, that is, Scur and Snext are symmetric state transitions, then retain the state transition of the target character input_char and do not replace the current state and the next state of the target character.
[0041] S103. Scan the forward string through the forward DFA and scan the reverse string through the reverse DFA, and obtain the overall scanning result during bidirectional scanning according to the respective scanning results.
[0042] In the embodiment of the present application, the reverse string is the reverse of the forward string. Scanning is performed through the bidirectional DFA, that is, the forward string T is scanned through the forward DFA, and the reverse string T' is scanned through the reverse DFA, and the overall scanning result during bidirectional scanning is obtained according to the respective scanning results.
[0043] As a possible implementation manner, when scanning the forward string through the forward DFA and scanning the reverse string through the reverse DFA, the forward string is scanned and matched in the order from left to right through the forward DFA to obtain the corresponding forward scanning result, and the first character position (i) where the currently scanned character is located is recorded, and the first character position (i) is incremented every time a character is processed subsequently; the reverse string is scanned and matched in the order from right to left through the reverse DFA to obtain the corresponding reverse scanning result, and the second character position (j) where the currently scanned character is located is recorded, and the second character position (j) is decremented every time a character is processed subsequently. Optionally, when incrementing the first character position, 1 can be added, and when decrementing the second character position, 1 can be subtracted.
[0044] Among them, the first character position represents the position of the character that has been scanned by the forward DFA in the forward string; the forward scanning result represents the state transition change during the scanning process of the forward DFA; the initial value of the first character position is 0; the second character position represents the position of the character that the reverse DFA is currently scanning in the reverse string; the reverse scanning result represents the state transition change during the scanning process of the reverse DFA; the initial value of the second character position is the length of the forward string or the reverse string minus 1.
[0045] As a possible implementation manner, when obtaining the overall scanning result during bidirectional scanning according to the respective scanning results, when the first character position is equal to the second character position, determine the current forward state of the forward DFA and the current reverse state of the reverse DFA (at the character at the first character position); in response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are symmetric states, the forward DFA and the reverse DFA stop scanning and matching, and the forward scanning result of the forward DFA at this time and the reverse order of the reverse scanning result of the reverse DFA are merged to obtain the merged overall scanning result. Among them, the overall scanning result represents the overall state transition change during the scanning process of the bidirectional DFA.
[0046] It can be understood that the equality of the first character position and the second character position means that the characters at the first character position and the second character position belong to the same character of the string.
[0047] Specifically, the forward DFA scans and matches the string T = "abcbcbc" in the order from left to right, and records the currently scanned character position i (the position in the forward string, ranging from 0 - 6, that is, the initial value is 0 and increases sequentially, such as performing an increment of 1 until it reaches 6). The reverse DFA scans and matches the reverse string T' = "cbcbcba" of the string T in the order from right to left, and records the currently scanned character position j (the position in the reverse string, ranging from 6 to 0, that is, the initial value is 6 and decreases sequentially, such as performing a decrement of 1 until it reaches 0).
[0048] Continuing, when the character positions i = j (indicating the same character, that is, the positions coincide), it is judged that if, at the character at position i, the current state Scur_forward of the forward DFA and the current state Scur_reverse of the reverse DFA are symmetric states, then the scanning result of the forward DFA at this time is merged with the forward order of the reverse scanning result of the reverse DFA. For example, when i = j = 4, it means that the state transition change of the character scanned by the forward DFA is S0 - S4, and the state transition change of the character scanned by the reverse DFA is S4 - S0. The respective state transition changes during the scanning process at this time, that is, the scanning results (the character at this position where the positions coincide and the previous scanning results) are merged. It should be noted that after the merger, there is no need for two-way scanning anymore, that is, both the forward DFA and the reverse DFA stop scanning, which is equivalent to the scanning speed being accelerated by about twice compared to the one-way scanning of the conventional original DFA.
[0049] Furthermore, in response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are non - symmetric states, the reverse DFA stops scanning and matching; the forward DFA continues to scan and match the forward string to the right until the forward string is scanned completely, and the corresponding scanning result is used as the overall scanning result, and the first hit rule mark is determined as the target hit rule mark.
[0050] It should be noted that when the character positions i = j, at the character at position i, if the current state Scur_forward of the forward DFA and the current state Scur_reverse of the reverse DFA are in a non - symmetric state, then the reverse DFA stops scanning and matching. At the same time, the forward DFA continues to scan to the right until the forward string T is scanned to the end. At this time, the scanning result of this forward DFA is the overall scanning result, without involving the merger of scanning results, which is equivalent to the one - way scanning process of the original DFA and has no acceleration effect.
[0051] For example, take Figure 2For example, assume that S0 = 0, the string T = "abcbcbc", then the scanning result of the forward DFA for "abcb" is (0, 0, 0, 1, 2), and the character position at this time is i = 3; assume that S7 = 0, the reverse string T' = "cbcbcba", since the reverse DFA scans and matches T' from right to left, the scanning result of the reverse DFA for "cbcb" is (0, 1, 2, 2, 1), and its corresponding forward order is (1, 2, 2, 1, 0), and the character position is j = 3; at this time, i = j = 3, the current input character is b, and the current forward state Scur_forward and the current reverse state Scur_reverse are symmetric states of the input character b, and directly merge (0, 0, 0, 1,2 ) and ( 1, 2 , 2, 1, 0) to get (0, 0, 0, 1, 2, 2, 1, 0), where the bold and underlined 1 and 2 are Scur_forward and Snext_forward of the last character b during the forward matching of the forward DFA, and also Scur_reverse and Snext_reverse of the last character b during the reverse matching of the reverse DFA.
[0052] Obviously, this application can include two stages. The first stage is the preprocessing stage, that is, obtaining the reverse DFA through the forward DFA, and using all the states of the forward DFA to process 256 ASCII characters respectively to obtain the reverse DFA; the other stage is the matching and scanning stage, that is, obtaining the scanning results by processing the string T and the reverse string T' through the forward DFA and the reverse DFA.
[0053] The bidirectional DFA scanning and matching method provided by the embodiments of the present application processes a target number of target characters based on all states of a preset forward DFA, determines whether the current state and the next-hop state corresponding to the target character are symmetric state transitions of the target character. If the current state and the next-hop state corresponding to the target character are not symmetric state transitions of the target character, the current state and the next-hop state of the target character are replaced based on a preset target state to obtain a reverse DFA corresponding to the forward DFA, and a reverse string corresponding to the preset forward string is determined. The forward string is scanned by the forward DFA, and the reverse string is scanned by the reverse DFA. The overall scanning result during bidirectional scanning is obtained according to the respective scanning results. In the bidirectional DFA scanning and matching method of the present application, when it is determined that the current state and the next-hop state of the target character processed by using all states of the forward DFA are non-symmetric state transitions, the current state and the next-hop state of the target character are replaced to obtain a reverse DFA corresponding to the forward DFA, and the string is scanned and matched from both the positive and negative directions by the forward DFA and the reverse DFA, that is, the forward DFA scans and matches the forward string from left to right, and the reverse DFA scans and matches the reverse string from right to left. Thus, the scanning speed of the DFA is improved, and the scanning performance and matching performance of the DFA are thereby improved.
[0054] Further, a first hit rule flag for determining whether a preset target rule is hit during the process of the forward DFA processing the forward string and a second hit rule flag to be determined during the process of the reverse DFA processing the reverse string are determined; in response to the current forward state of the forward DFA and the current reverse state of the reverse DFA being symmetric states, a target hit rule flag for finally determining whether the forward string hits the target rule is obtained based on the first hit rule flag and the second hit rule flag; and whether the forward string hits the target rule is determined based on the target hit rule flag.
[0055] It should be noted that in response to the transfer state accessed during the process of the forward DFA scanning the forward string being a preset acceptable state, the first hit rule flag is assigned; in response to the transfer state accessed during the process of the reverse DFA scanning the reverse string being an acceptable state, the second hit rule flag is assigned; in response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are symmetric states, an OR operation is performed on the first hit rule flag and the second hit rule flag to obtain the target hit rule flag.
[0056] Specifically, i = 0 (the initial value is 0), j = n - 1, where n is the length of the forward string T or the reverse string T' to be matched. The first hit rule flag sure_match = 0, the second hit rule flag doubt_match = 0, and the target hit rule flag final_match = 0. Among them, the sure_match flag indicates whether the rule is hit during the process of the forward DFA processing the string. 0 means not hit, and 1 means hit; the doubt_match is the hit rule flag to be determined during the process of the reverse DFA processing the string; the final_match is the flag for finally determining whether the string hits the rule.
[0057] Continuing, the forward DFA scans and matches the string T in the order from left to right, and records the current character position i being matched. For each character processed, i++ is executed. If there is an acceptable state (such as 2) during the scanning process, set sure_match = 1; assuming the initial state is 0, the reverse DFA scans and matches the reverse string T' in the order from right to left, and records the reverse position j of the current character being matched. For each character processed, j-- is executed. If there is an acceptable state (such as 2) during the reverse scanning process of the reverse DFA, set doubt_match = 1.
[0058] Finally, when i = j, if on the character at position i, the current state Scur_forward of the forward DFA and the current state Scur_reverse of the reverse DFA are symmetric states, then final_match = sure_match | doubt_match, that is, the target hit rule flag is obtained through an OR operation. Otherwise, the reverse DFA' stops scanning, and the forward DFA continues to scan to the right until the scanning of T ends. At this time, final_match = sure_match. Thus, based on final_match, it is determined whether the rule is hit. For example, when final_match is 1, it means the rule is hit; when final_match is 0, it means the rule is not hit.
[0059] It can be added that in response to the process of the reverse DFA scanning the reverse string, if the accessed transition state is the target state, the scanning of the reverse DFA stops. That is, if during the reverse scanning process, the accessed transition state is the target state N of the above replacement, the reverse DFA stops scanning.
[0060] It should be noted that a two-way DFA scanning and matching method of the present application, that is, a method for a two-way DFA to scan a string, is also a data two-way processing method.
[0061] Figure 4is a schematic structural diagram of a bidirectional DFA scanning and matching device provided according to an embodiment of the present application; as Figure 4 shown, the bidirectional DFA scanning and matching device 400 according to the embodiment of the present application may specifically include: A first determination module 401, configured to process a target number of target characters based on all states of a preset forward DFA respectively, and determine whether the current state and the next-hop state corresponding to the target character are symmetric state transitions of the target character.
[0062] A replacement module 402, configured to, if the current state and the next-hop state corresponding to the target character are not symmetric state transitions of the target character, replace the current state and the next-hop state of the target character based on a preset target state, obtain a reverse DFA corresponding to the forward DFA, and determine a reverse string corresponding to a preset forward string.
[0063] A scanning module 403, configured to scan a forward string through the forward DFA, and scan a reverse string through the reverse DFA, and obtain an overall scanning result during bidirectional scanning according to the respective scanning results; wherein, the reverse string is the reverse string of the forward string.
[0064] In a possible implementation manner, the first determination module is specifically configured to: In response to the forward DFA, when it is established that the forward DFA jumps from the current state to the next-hop state when processing the target character, determine whether it is established that the forward DFA jumps from the next-hop state to the current state when processing the target character; If it is established that the forward DFA jumps from the next-hop state to the current state when processing the target character, the current state and the next-hop state are symmetric state transitions of the target character.
[0065] In a possible implementation manner, the scanning module is specifically configured to: Scan and match the forward string through the forward DFA in the order from left to right, obtain a corresponding forward scanning result, record the position of the first character where the currently scanned character is located, and increment the position of the first character every time a subsequent character is processed; Scan and match the reverse string through the reverse DFA in the order from right to left, obtain a corresponding reverse scanning result, record the position of the second character where the currently scanned character is located, and decrement the position of the second character every time a subsequent character is processed. In a possible implementation manner, the scanning module is specifically configured to: When the position of the first character is equal to the position of the second character, determine that the current forward state of the forward DFA and the current reverse state of the reverse DFA are reverse; In response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are symmetric states, the forward DFA and the reverse DFA stop scanning and matching, and the forward scanning result of the forward DFA at this time and the forward order of the reverse scanning result of the reverse DFA are merged to obtain the merged overall scanning result.
[0066] In a possible implementation, the bidirectional DFA scanning and matching device includes: A second determination module, configured to determine whether a first hit rule flag of a preset target rule is hit during the process of the forward DFA processing the forward string, and a second hit rule flag to be determined during the process of the reverse DFA processing the reverse string; An acquisition module, configured to, in response to the current forward state of the forward DFA and the current reverse state of the reverse DFA being symmetric states, obtain a target hit rule flag for finally determining whether the forward string hits the target rule based on the first hit rule flag and the second hit rule flag; A third determination module, configured to determine whether the forward string hits the target rule based on the target hit rule flag.
[0067] In a possible implementation, the bidirectional DFA scanning and matching device includes: A first assignment module, configured to, in response to the transfer state accessed during the process of the forward DFA scanning the forward string being a pre-determined acceptable state, assign a value to the first hit rule flag; A second assignment module, configured to, in response to the transfer state accessed during the process of the reverse DFA scanning the reverse string being an acceptable state, assign a value to the second hit rule flag; A calculation module, configured to, in response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are symmetric states, perform an OR operation on the first hit rule flag and the second hit rule flag to obtain the target hit rule flag. In a possible implementation, the bidirectional DFA scanning and matching device includes: A fourth determination module, configured to, in response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are asymmetric states, stop the reverse DFA from scanning and matching; A fifth determination module, configured to continue to scan and match the forward string to the right through the forward DFA until the forward string is scanned completely, obtain the corresponding scanning result as the overall scanning result, and determine the first hit rule flag as the target hit rule flag.
[0068] The two-way DFA scanning and matching device provided by the embodiment of the present application processes a target number of target characters based on all states of a preset forward DFA, determines whether the current state and the next-hop state corresponding to the target character are symmetric state transitions of the target character. If the current state and the next-hop state corresponding to the target character are not symmetric state transitions of the target character, the current state and the next-hop state of the target character are replaced based on a preset target state to obtain a reverse DFA corresponding to the forward DFA, and a reverse string corresponding to a preset forward string is determined. The forward string is scanned by the forward DFA, and the reverse string is scanned by the reverse DFA, and the overall scanning result during two-way scanning is obtained according to the respective scanning results. The two-way DFA scanning and matching device of the present application, when determining that the current state and the next-hop state of the target character processed by using all states of the forward DFA are non-symmetric state transitions, replaces the current state and the next-hop state of the target character to obtain a reverse DFA corresponding to the forward DFA, and scans and matches the string in both the forward and reverse directions through the forward DFA and the reverse DFA, that is, the forward DFA scans and matches the forward string from left to right, and the reverse DFA scans and matches the reverse string from right to left. Thus, the scanning speed of the DFA is improved, and the scanning performance and matching performance of the DFA are thereby improved.
[0069] As Figure 5 shown, an electronic device 500 provided by an embodiment of the present application includes: a processor 501, a memory 502, and a bus. The memory 502 stores machine-readable instructions executable by the processor 501. When the electronic device runs, the processor 501 communicates with the memory 502 through the bus, and the processor 501 executes the machine-readable instructions to perform the steps of the two-way DFA scanning and matching method as described above.
[0070] Specifically, the above-mentioned memory 502 and processor 501 can be general-purpose memory and processor, which are not specifically limited here. When the processor 501 runs the computer program stored in the memory 502, it can execute the two-way DFA scanning and matching method as described above.
[0071] Corresponding to the above two-way DFA scanning and matching method, an embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is run by a processor, it executes the steps of the above two-way DFA scanning and matching method.
[0072] Those skilled in the art can clearly understand that for the convenience and conciseness of description, the specific working processes of the systems and devices described above can refer to the corresponding processes in the method embodiments, and will not be elaborated herein. In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of the modules is only a logical function division, and there may be other division methods in actual implementation. For another example, multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the couplings or direct couplings or communication connections shown or discussed with each other can be through some communication interfaces. The indirect couplings or communication connections of the devices or modules can be in electrical, mechanical or other forms.
[0073] The modules described as separate components may or may not be physically separated. The components shown as modules 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 solution of this embodiment.
[0074] In addition, in each embodiment of the present application, the functional units can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit.
[0075] If the functions are implemented in the form of software function units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium executable by a processor. Based on such an understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the deployment methods described in each embodiment of the present application. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, ROM, RAM, magnetic disks or optical discs that can store program codes.
[0076] The above are only the specific implementation manners of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed in the present application, and all should be covered by the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.
Claims
1. A bidirectional DFA scanning matching method, characterized in that: The method comprises: Based on all states of the preset forward DFA, a target number of target characters are processed respectively to determine whether the current state and the next hop state corresponding to the target character are symmetric state transfers of the target character; If the current state and the next hop state corresponding to the target character are not symmetric state transfers of the target character, the current state and the next hop state of the target character are replaced based on the preset target state to obtain the reverse DFA corresponding to the forward DFA, and determine the reverse string corresponding to the preset forward string; The forward string is scanned by the forward DFA, and the reverse string is scanned by the reverse DFA, and the overall scanning result of bidirectional scanning is obtained according to the respective scanning results; wherein the reverse string is the reverse string of the forward string.
2. The method according to claim 1, characterized in that The step of determining whether the current state and the next hop state corresponding to the target character are symmetric state transitions of the target character includes: In response to the forward DFA, when the forward DFA processes the target character, jumping from the current state to the next state is established, determining whether the forward DFA processes the target character, jumping from the next state to the current state is established; If the forward DFA jumps from the next hop state to the current state when processing the target character, the current state and the next hop state are symmetric state transfers of the target character.
3. The method according to claim 2, characterized in that The scanning of the forward string by the forward DFA and the scanning of the reverse string by the reverse DFA include: Scan and match the forward character string from left to right through the forward DFA to obtain the corresponding forward scanning result, and record the first character position of the character currently being scanned, and increment the first character position each time a character is processed subsequently; The reverse character string is scanned and matched from right to left by the reverse DFA to obtain the corresponding reverse scanning result, and the second character position of the character currently being scanned is recorded, and the second character position is decremented each time a character is processed subsequently.
4. The method according to claim 3, characterized in that The overall scanning result of bidirectional scanning is obtained according to the respective scanning results, including: When the first character position is equal to the second character position, determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are reversed; In response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are symmetrical states, the forward DFA and the reverse DFA stop scanning and matching, and merge the forward scanning result of the forward DFA and the reverse scanning result of the reverse DFA in positive order to obtain a merged overall scanning result.
5. The method according to claim 4, characterized in that The method further comprises: Determine a first hit rule mark of whether a preset target rule is hit during the forward DFA processing of the forward string, and a second hit rule mark to be determined during the reverse DFA processing of the reverse string; In response to the current forward state of the forward DFA and the current reverse state of the reverse DFA being in a symmetric state, a target hit rule mark for finally determining whether the forward string hits the target rule is obtained based on the first hit rule mark and the second hit rule mark; Determine whether the forward string hits the target rule based on the target hit rule flag.
6. The method according to claim 5, characterized in that The method further comprises: In response to the forward DFA scanning the forward string, the accessed transition state is a predetermined acceptable state, assigning a value to the first hit rule flag; In response to the accessed transition state being the acceptable state during the process of the reverse DFA scanning the reverse string, assigning a value to the second hit rule flag; In response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are symmetric states, an OR operation is performed on the first hit rule flag and the second hit rule flag to obtain the target hit rule flag.
7. The method according to claim 6, characterized in that The method further comprises: In response to determining that the current forward state of the forward DFA and the current reverse state of the reverse DFA are asymmetric states, the reverse DFA stops scanning and matching; The forward DFA continues to scan and match the forward character string to the right until the forward character string is completely scanned, and a corresponding scanning result is obtained as the overall scanning result, and the first hit rule mark is determined as the target hit rule mark.
8. A bidirectional DFA scanning matching device, characterized in that: The device comprises: A first determination module is used to process a target number of target characters respectively based on all states of a preset forward DFA, and determine whether a current state and a next hop state corresponding to the target character are symmetric state transfers of the target character; A replacement module, for replacing the current state and the next hop state of the target character based on a preset target state if the current state and the next hop state corresponding to the target character are not symmetric state transfers of the target character, obtaining a reverse DFA corresponding to the forward DFA, and determining a reverse string corresponding to a preset forward string; The scanning module is used to scan the forward string through the forward DFA and scan the reverse string through the reverse DFA, and obtain the overall scanning result of bidirectional scanning according to the respective scanning results; wherein the reverse string is the reverse string of the forward string.
9. An electronic device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the bidirectional DFA scan matching method as described in any one of claims 1 to 7 are performed.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the bidirectional DFA scan matching method according to any one of claims 1 to 7 are executed.
Citation Information
Patent Citations
Regular expression matching method and regular expression matching device based on template-based multidimensional finite automaton TMFA
CN106878289A
Power Internet of Things terminal flow anomaly analysis method based on protocol reverse analysis
CN113452672A
Positionally dependent pattern checking in character strings using deterministic finite automata
GB0617232D0
DFA sub-scans
US20140215090A1