A string matching method, apparatus and related device

By defining a state index field in the AC state machine and storing only the directly associated pattern string ID, the problem of high memory consumption in traditional algorithms is solved, and more efficient string matching performance is achieved.

CN123178683APending Publication Date: 2026-11-27NEW H3C TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611298629.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-25
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

The traditional Aho-Corasick algorithm consumes a lot of memory in multi-pattern string matching due to storing a large number of pattern string IDs, which affects system performance and cache hit rate, especially when dealing with a large number of patterns and large-scale data.

Method used

In each state data structure of the AC state machine, a field is defined to point to the index of the state containing the pattern string ID that is closest to the current state on the Fail chain. Only the pattern string ID directly associated with the current state is stored, and the jump is performed through this field to avoid inheriting all pattern string IDs.

Benefits of technology

Without increasing time complexity, this method reduces memory consumption, improves cache hit rate, and enhances the overall performance of multi-pattern string matching.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123178683A_ABST
    Figure CN123178683A_ABST
Patent Text Reader

Abstract

The application relates to the technical field of data processing, in particular to a string matching method and device and related equipment. A first field for pointing to a state containing a pattern string ID closest to the state on a Fail chain is defined in a data structure of each state; when a current state is traversed based on a to-be-matched string, a pattern string ID directly associated with the current state is acquired, and when it is judged that the first field of the current state is a non-0 value, the pattern string ID directly associated with a first state containing a pattern string ID closest to the current state on the Fail chain is acquired based on the first field of the current state; and when it is judged that the first field of the first state is a non-0 value, the pattern string ID directly associated with a second state containing a pattern string ID closest to the first state on the Fail chain is recursively acquired, and whether the first field of the second state is a non-0 value is judged, until it is judged that the first field of a state is a 0 value, and the recording of the pattern string ID is ended.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data processing technology, and in particular to a string matching method, apparatus and related equipment. Background Technology

[0002] With the rapid growth of data volume and the increasing number of cyber threats, fast and effective multi-pattern string matching technology has become crucial in network security and data analysis. The Aho-Corasick (AC) algorithm, due to its efficient multi-pattern matching capabilities, is widely used in deep packet inspection, firewalls, intrusion detection systems, and text processing.

[0003] Traditional Acoustic Comparison (AC) algorithms achieve efficient matching of multiple pattern strings by constructing a Trie tree and Fail pointers. In this algorithm, the Fail pointers are used to backtrack to the next possible matching state when a match fails. However, to avoid repeated jumps along the Fail chain, the common practice is to inherit the pattern IDs along the Fail chain. Each node (state) stores all the pattern string IDs (PIDs) it inherits along the Fail chain. While this method reduces the number of jumps during the matching process, it leads to significant memory consumption.

[0004] This excessive memory consumption places a significant burden on resource-constrained environments, impacting system performance and efficiency. Optimizing memory usage is particularly crucial in applications that handle numerous patterns and large-scale data. Furthermore, excessive memory usage can lead to a decrease in cache hit rate, thus affecting overall performance. Summary of the Invention

[0005] This application provides a string matching method, apparatus, and related devices.

[0006] Firstly, this application provides a string matching method, the method comprising: When constructing the AC state machine, a first field is defined in the data structure of each state to point to the index of the state containing the pattern string ID that is closest to that state on the Fail chain. When traversing the AC state machine based on the string to be matched, and traversing to the current state, the pattern string ID directly associated with the current state is obtained, and it is determined whether the first field included in the data structure of the current state is a non-zero value. If the first field of the data structure of the current state is determined to be a non-zero value, then based on the first field of the data structure of the current state, obtain the pattern string ID that is directly associated with the first state containing the pattern string ID on the Fail chain that is closest to the current state; and When it is determined that the first field of the data structure of the first state is a non-zero value, the pattern string ID directly associated with the second state containing the pattern string ID that is closest to the first state on the Fail chain is obtained, and it is determined whether the first field of the data structure of the second state is a non-zero value. The pattern string ID recording ends when it is determined that the first field of the data structure of a state is 0.

[0007] Optionally, a state's data structure may also include a second field for storing pattern string ID information directly associated with that state; The steps for obtaining the pattern string ID directly associated with the current state include: Read the second field of the data structure of the current state; Based on the second field included in the data structure of the current state, obtain the pattern string ID directly associated with the current state.

[0008] Optionally, if a second field in the data structure of a state is used to directly store the pattern string ID directly associated with that state, then the step of obtaining the pattern string ID directly associated with the current state based on the second field in the data structure of the current state includes: The pattern string ID stored in the second field of the data structure of the current state is used as the pattern string ID directly associated with the current state; If a state's data structure includes a second field used to store a pointer to a pattern string ID directly associated with that state, stored independently, then the step of obtaining the pattern string ID directly associated with the current state based on the second field of the current state's data structure includes: Based on the second field of the data structure of the current state, a pointer to the independently stored pattern string ID directly associated with the current state is stored, and the pattern string ID directly associated with the current state is obtained.

[0009] Optionally, a state's data structure further includes a third field for storing the number of pattern string IDs directly associated with that state; the method further includes: Read the third field included in the data structure of the current state; When the number of pattern string IDs directly associated with the current state is determined to be 0 based on the value of the third field included in the data structure of the current state, the step of determining whether the first field included in the data structure of the current state is a non-zero value is executed. When the number of pattern string IDs directly associated with the current state is determined to be n based on the value of the third field included in the data structure of the current state, the second field included in the data structure of the previous state is read to obtain the n pattern string IDs directly associated with the current state, where n is a positive integer greater than or equal to 1.

[0010] Optionally, if a state is the state corresponding to the last character of any preset string, then the third field of the data structure of the state is a non-zero value, and the second field of the data structure of the state stores the pattern string ID information associated with the preset string corresponding to the state. If a state is not the state corresponding to the last character of the preset string, then the third field of the data structure for that state will have a value of 0.

[0011] Secondly, this application provides a string matching apparatus, the apparatus comprising: The building unit is used to define a first field in the data structure of each state when building the AC state machine, which is used to point to the index of the state containing the pattern string ID that is closest to the state on the Fail chain. The acquisition unit is used to acquire the pattern string ID directly associated with the current state when traversing the AC state machine based on the string to be matched and reaching the current state. The judgment unit is used to determine whether the first field included in the data structure of the current state is a non-zero value; If the determining unit determines that the first field included in the data structure of the current state is a non-zero value, the obtaining unit is further configured to, based on the first field included in the data structure of the current state, obtain the pattern string ID directly associated with the first state containing the pattern string ID on the Fail chain that is closest to the current state; and If the determination unit determines that the first field included in the data structure of the first state is a non-zero value, the acquisition unit is further configured to acquire the pattern string ID directly associated with the nearest state containing the pattern string ID on the Fail chain. The determination unit is further configured to determine whether the first field included in the data structure of the second state is a non-zero value, until the first field included in the data structure of a state is determined to be 0, and then the pattern string ID recording ends.

[0012] Optionally, a state's data structure may also include a second field for storing pattern string ID information directly associated with that state; When obtaining the pattern string ID directly associated with the current state, the obtaining unit is specifically used for: Read the second field of the data structure of the current state; Based on the second field included in the data structure of the current state, obtain the pattern string ID directly associated with the current state.

[0013] Optionally, if a second field in the data structure of a state is used to directly store the pattern string ID directly associated with that state, then when obtaining the pattern string ID directly associated with the current state based on the second field in the data structure of the current state, the obtaining unit is specifically used for: The pattern string ID stored in the second field of the data structure of the current state is used as the pattern string ID directly associated with the current state; If a state's data structure includes a second field used to store a pointer to a pattern string ID directly associated with that state, stored independently, then when obtaining the pattern string ID directly associated with the current state based on the second field of the current state's data structure, the obtaining unit is specifically used for: Based on the second field of the data structure of the current state, a pointer to the independently stored pattern string ID directly associated with the current state is stored, and the pattern string ID directly associated with the current state is obtained.

[0014] Optionally, a state's data structure may also include a third field for storing the number of pattern string IDs directly associated with that state; The acquisition unit is further configured to read the third field included in the data structure of the current state; When the judgment unit determines that the number of pattern string IDs directly associated with the current state is 0 based on the value of the third field included in the data structure of the current state, it performs the step of judging whether the first field included in the data structure of the current state is a non-zero value. When the judging unit determines that the number of pattern string IDs directly associated with the current state is n based on the value of the third field included in the data structure of the current state, the obtaining unit is used to read the second field included in the data structure of the previous state to obtain the n pattern string IDs directly associated with the current state. The judging unit is also used to perform the step of judging whether the first field included in the data structure of the current state is a non-zero value, where n is a positive integer greater than or equal to 1.

[0015] Optionally, if a state is the state corresponding to the last character of any preset string, then the third field of the data structure of the state is a non-zero value, and the second field of the data structure of the state stores the pattern string ID information associated with the preset string corresponding to the state. If a state is not the state corresponding to the last character of the preset string, then the third field of the data structure for that state will have a value of 0.

[0016] Thirdly, embodiments of this application provide a string matching device, which includes: Memory, used to store program instructions; A processor is configured to invoke program instructions stored in the memory and execute the steps of the method as described in any one of the first aspects above, according to the obtained program instructions.

[0017] Fourthly, embodiments of this application also provide a computer-readable storage medium storing computer-executable instructions for causing the computer to perform the steps of the method as described in any of the first aspects above.

[0018] In summary, the string matching method provided in this application defines a first field in the data structure of each state when constructing the AC state machine, which points to the state index containing the pattern string ID closest to the current state on the Fail chain. When traversing the AC state machine based on the string to be matched, and reaching the current state, the pattern string ID directly associated with the current state is obtained, and it is determined whether the first field included in the data structure of the current state is a non-zero value. If the first field included in the data structure of the current state is determined to be a non-zero value, the pattern string ID directly associated with the first state containing the pattern string ID closest to the current state on the Fail chain is obtained based on the first field included in the data structure of the current state. When the first field included in the data structure of the first state is determined to be a non-zero value, the pattern string ID directly associated with the second state containing the pattern string ID closest to the first state on the Fail chain is obtained, and it is determined whether the first field included in the data structure of the second state is a non-zero value, until the first field included in the data structure of a state is determined to be a zero value, at which point the pattern string ID recording ends.

[0019] The string matching method provided in this application only stores the pattern string IDs directly associated with itself in the data structure of each state. Each state's data structure also defines a field that stores the identifier of the state containing the pattern string ID closest to it on its corresponding Fail chain. During string matching, the system jumps based on this field of each state to collect the pattern string IDs that should be matched on its corresponding Fail chain. Each state does not need to inherit all pattern string IDs on its corresponding Fail chain. This reduces memory consumption and improves cache hit rate without increasing time complexity or affecting query efficiency, thereby improving overall performance in multi-pattern string matching. Attached Figure Description

[0020] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments of this application or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this application. For those skilled in the art, other drawings can be obtained based on these drawings of the embodiments of this application.

[0021] Figure 1 A detailed flowchart of a string matching method provided in an embodiment of this application; Figure 2 A schematic diagram of an AC state machine provided for an embodiment of this application; Figure 3 This is a schematic diagram of the structure of a string matching device provided in an embodiment of this application; Figure 4 This is a schematic diagram of the hardware architecture of a string matching device provided in an embodiment of this application. Detailed Implementation

[0022] The terminology used in the embodiments of this application is for the purpose of describing particular embodiments only and is not intended to limit the application. The singular forms “a,” “the,” and “the” as used in this application and 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 any and all possible combinations comprising one or more of the associated listed items.

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

[0024] For example, see Figure 1 The diagram shown is a detailed flowchart of a string matching method provided in an embodiment of this application. The method includes the following steps: Step 100: When constructing the AC state machine, define a first field in the data structure of each state to point to the index of the state containing the pattern string ID that is closest to that state on the Fail chain.

[0025] For example, see Figure 2As shown, multiple pattern strings (preset strings) are inserted into the Trie tree. Each node records its state ID (e.g., 1, 2, 3, 4, 5, 6, 7). At the end position of the pattern string (the node / state corresponding to the last character of the preset string), the pattern string ID corresponding to the pattern string is added to the PID list corresponding to the last string of the state (e.g., the pattern string ID of state 6 corresponding to the last character "c" of the preset string "abc" is 1, the pattern string ID of state 7 corresponding to the last character "d" of the preset string "bcd" is 2, and the pattern string ID of state 3 corresponding to the preset string "c" is 3).

[0026] In this embodiment of the application, when constructing the AC state machine, for each state (e.g., 1, 2, 3, 4, 5, 6, 7), a first field is defined in the data structure of that state to point to the index of the state containing the pattern string ID on the corresponding Fail chain that is closest to that state.

[0027] For example, with Figure 2 Taking the example shown, states 3, 6, and 7 are states containing a pattern string ID. For state 4, its corresponding fail chain is: 4->2; for state 6, its corresponding fail chain is: 6->5, 5->3; for state 5, its corresponding fail chain is: 5->3. For state 4, there is no state containing a pattern string ID on its corresponding fail chain, so the first field value in the data structure of state 4 is null (value is 0). For state 6, there is a state containing a pattern string ID (state 3) on its corresponding fail chain, and state 3 is the closest state containing a pattern string ID to state 6, so the first field value in the data structure of state 6 is an index pointer to state 6. For state 5, there is a state containing a pattern string ID (state 3) on its corresponding fail chain, and state 3 is the closest state containing a pattern string ID to state 5, so the first field value in the data structure of state 5 is an index pointer to state 6.

[0028] Step 110: When traversing the AC state machine based on the string to be matched, when traversing to the current state, obtain the pattern string ID directly associated with the current state, and determine whether the first field included in the data structure of the current state is a non-zero value.

[0029] In this embodiment of the application, the data structure of a state may further include a second field for storing pattern string ID information directly associated with the state.

[0030] In other words, the second field of the data structure for each state stores the pattern string ID directly associated with that state. If a state does not have a directly associated pattern string ID, that is, if the state is not the state corresponding to the last character of any preset string, then the value of the second field of the data structure for that state is empty. If a state is the state corresponding to the last character of a preset string, then the second field of the data structure for that state stores the pattern string ID directly associated with that state.

[0031] Therefore, when obtaining the pattern string ID directly associated with the current state, a preferred implementation is as follows: Read the second field included in the data structure of the current state; based on the second field included in the data structure of the current state, obtain the pattern string ID directly associated with the current state.

[0032] In this embodiment of the application, a preferred implementation is that the second field of a state's data structure is used to directly store the pattern string ID directly associated with that state.

[0033] For example, see still Figure 2 As shown, state 6 corresponds to the last character "c" of the preset string "abc", and the value of the second field of the data structure of state 6 is the pattern string ID corresponding to the preset string "abc" (1); state 7 corresponds to the last character "d" of the preset string "bcd", and the value of the second field of the data structure of state 7 is the pattern string ID corresponding to the preset string "bcd" (2); state 3 corresponds to the preset string "c", and the value of the second field of the data structure of state 3 is the pattern string ID corresponding to the preset string "c" (3).

[0034] Therefore, when obtaining the pattern string ID directly associated with the current state based on the second field included in the data structure of the current state, a preferred implementation is as follows: The pattern string ID stored in the second field of the data structure of the current state is used as the pattern string ID directly associated with the current state.

[0035] In another preferred implementation of this application, a data structure for a state includes a second field used to store a pointer to a pattern string ID directly associated with the state, which is stored independently.

[0036] For example, see still Figure 2As shown, state 6 corresponds to the last character "c" of the preset string "abc", and the value of the second field of the data structure of state 6 is a pointer to the pattern string ID (1) corresponding to the preset string "abc" stored independently; state 7 corresponds to the last character "d" of the preset string "bcd", and the value of the second field of the data structure of state 7 is a pointer to the pattern string ID (2) corresponding to the preset string "bcd" stored independently; state 3 corresponds to the preset string "c", and the value of the second field of the data structure of state 3 is a pointer to the pattern string ID (3) corresponding to the preset string "c".

[0037] Therefore, when obtaining the pattern string ID directly associated with the current state based on the second field included in the data structure of the current state, a preferred implementation is as follows: Based on the second field of the data structure of the current state, a pointer to the independently stored pattern string ID directly associated with the current state is stored, and the pattern string ID directly associated with the current state is obtained.

[0038] In other words, a pointer to the pattern string ID directly associated with the current state is obtained from the second field of the current state's data structure, which is stored independently. Then, the pattern string ID directly associated with the current state is obtained from the independent storage based on this pointer.

[0039] Furthermore, in this embodiment of the application, the data structure of a state may also include a third field for storing the number of pattern string IDs directly associated with that state.

[0040] Therefore, in this embodiment of the application, when obtaining the pattern string ID directly associated with the current state, a preferred implementation is as follows: read the third field included in the data structure of the current state; when it is determined that the number of pattern string IDs directly associated with the current state is 0 based on the value of the third field included in the data structure of the current state, perform the step of determining whether the first field included in the data structure of the current state is a non-zero value; when it is determined that the number of pattern string IDs directly associated with the current state is n based on the value of the third field included in the data structure of the current state, read the second field included in the data structure of the previous state to obtain the n pattern string IDs directly associated with the current state, where n is a positive integer greater than or equal to 1.

[0041] For example, Table 1 illustrates the fields included in the state data structure, where... The “uiStateID” field is a unique identifier for the state, used to uniquely identify the state. The "uiFailedState" field is a Fail pointer that points to the next state when the current state fails to match. The “uiPidNum” field records the number of pattern string IDs (PIDs in the PID list) directly associated with the current state, with a maximum support of 511. The “uiLastPidFailedState” field points to the unique identifier of the state containing the pattern string ID that is closest to this state on the Fail chain; The “PidList” field is a list of PIDs that stores information about the pattern string IDs that directly match the current state.

[0042] The pattern string ID can be a pointer to a list of PIDs, pointing to an independently stored list of PIDs; or, the pattern string ID can directly store each PID directly associated with that state.

[0043] Table 1 The following explanation uses the data structure of state 6 (the state corresponding to the last character of a preset string) as an example. See Table 2 for a schematic of the data structure for state 6: Table 2 The following explanation uses the data structure of state 5 (which is not the last character of any preset string) as an example. See Table 3 for a schematic diagram of the data structure for state 5: Table 3 In this embodiment of the application, for each state included in the AC state machine, if a state is the state corresponding to the last character of any preset string, then the third field of the data structure of that state is a non-zero value, and the second field of the data structure of that state stores the pattern string ID information associated with the preset string corresponding to that state; if a state is not the state corresponding to the last character of the preset string, then the third field of the data structure of that state is a zero value, and correspondingly, the second field of the data structure of that state is also a zero value.

[0044] In other words, the value of the third field in the data structure of a state represents the number of pattern string IDs directly associated with that state. So, when traversing to a state, first read the third field in the data structure of that state. If the value of the third field is 0, it means that the state is not the state corresponding to the last character of the preset string, and the state does not have a directly associated pattern string ID. If the value of the third field is non-zero, it means that the state is the state corresponding to the last character of at least one preset string. In this case, it is necessary to read the second field of the data structure of that state to obtain the pattern string ID directly associated with that state.

[0045] The acquisition method is determined based on the specific data stored in the second field. If the second field directly stores the pattern string ID directly associated with the corresponding state, then the pattern string ID directly associated with the corresponding state is directly obtained. If the second field stores a pointer to the pattern string ID directly associated with the corresponding state in independent storage, then the pattern string ID directly associated with the corresponding state needs to be obtained from the independent storage based on the pointer.

[0046] Step 120: If the first field of the data structure of the current state is determined to be non-zero, then based on the first field of the data structure of the current state, obtain the pattern string ID that is directly associated with the first state containing the pattern string ID on the Fail chain that is closest to the current state.

[0047] For example, see still Figure 2 As shown in the embodiment of this application, it is assumed that the current traversal state is state 6, which is the state corresponding to the last character "c" of the preset string "abc". The value of the third field of the data structure of state 6 is 1, the second field stores the information of the pattern string ID (1) directly associated with state 6, and the first field points to state 3, which is closest to state 6 on the corresponding Fail chain and contains the pattern string ID. Then, while reading the pattern string ID (1) directly associated with state 6, it is possible to jump to state 3 based on the value of the first field included in the data structure of state 3, and obtain the pattern string ID (3) directly associated with state 3 based on the third field and the second field included in the data structure of state 3.

[0048] Assuming the current traversal state is state 4, and state 4 is not the state corresponding to the last character of any preset string, then the value of the third field of the data structure of state 4 is 0 (state 4 does not have a directly associated pattern string ID), and the value of the second field is also 0. Furthermore, there are no other states containing pattern string IDs on the Fail chain corresponding to state 4.

[0049] Step 130: When it is determined that the first field included in the data structure of the first state is a non-zero value, execute the process of obtaining the pattern string ID directly associated with the second state containing the pattern string ID that is closest to the first state on the Fail chain, and determine whether the first field included in the data structure of the second state is a non-zero value, until it is determined that the first field included in the data structure of a state is a zero value, and then end the pattern string ID recording.

[0050] As mentioned above, please refer to [the relevant documentation]. Figure 2 As shown, based on the value of the first field of the data structure of state 3 (first state), jump to state 3. Based on the third field and the second field of the data structure of state 3, obtain the pattern string ID directly associated with state 3 (3). While obtaining the pattern string ID directly associated with state 3, determine whether the value of the first field of the data structure of state 3 is non-zero. If it is non-zero, it means that there are other states containing pattern string IDs on the corresponding Fail chain. Continue to obtain the pattern string ID directly associated with the state containing pattern string ID closest to state 3 on the Fail chain, and determine whether the first field of the data structure of the second state is non-zero. If the first field of the data structure of the second state is zero, end the pattern string ID recording.

[0051] Based on the same inventive concept as the above-described embodiments, see, for example, the following: Figure 3 The diagram shown is a structural schematic of a string matching device provided in an embodiment of this application. The device includes: Construction unit 30 is used to define a first field in the data structure of each state when constructing the AC state machine, which is used to point to the index of the state containing the pattern string ID that is closest to the state on the Fail chain. The acquisition unit 31 is used to acquire the pattern string ID directly associated with the current state when traversing the AC state machine based on the string to be matched and reaching the current state. Judgment unit 32 is used to determine whether the first field included in the data structure of the current state is a non-zero value; If the judgment unit 32 determines that the first field included in the data structure of the current state is a non-zero value, then the acquisition unit 31 is further configured to, based on the first field included in the data structure of the current state, acquire the pattern string ID directly associated with the first state containing the pattern string ID on the Fail chain that is closest to the current state; and If the determination unit determines that the first field included in the data structure of the first state is a non-zero value, the acquisition unit 31 is further configured to acquire the pattern string ID directly associated with the second state containing the pattern string ID that is closest to the first state on the Fail chain. The determination unit 32 is further configured to determine whether the first field included in the data structure of the second state is a non-zero value, until the first field included in the data structure of a state is determined to be a zero value, and then the pattern string ID recording ends.

[0052] Optionally, a state's data structure may also include a second field for storing pattern string ID information directly associated with that state; When obtaining the pattern string ID directly associated with the current state, the obtaining unit 31 is specifically used for: Read the second field of the data structure of the current state; Based on the second field included in the data structure of the current state, obtain the pattern string ID directly associated with the current state.

[0053] Optionally, if the second field of a state's data structure is used to directly store the pattern string ID directly associated with that state, then when obtaining the pattern string ID directly associated with the current state based on the second field of the current state's data structure, the obtaining unit 31 is specifically used for: The pattern string ID stored in the second field of the data structure of the current state is used as the pattern string ID directly associated with the current state; If the second field of a state's data structure is used to store a pointer to a pattern string ID directly associated with that state and stored independently, then when reading the second field of the current state's data structure to obtain the pattern string ID directly associated with the current state, the obtaining unit 31 is specifically used for: Based on the second field of the data structure of the current state, a pointer to the independently stored pattern string ID directly associated with the current state is stored, and the pattern string ID directly associated with the current state is obtained.

[0054] Optionally, a state's data structure may also include a third field for storing the number of pattern string IDs directly associated with that state; The acquisition unit 31 is further configured to read the third field included in the data structure of the current state; When the judgment unit 32 determines that the number of pattern string IDs directly associated with the current state is 0 based on the value of the third field included in the data structure of the current state, it performs the step of judging whether the first field included in the data structure of the current state is a non-zero value. When the judgment unit 32 determines that the number of pattern string IDs directly associated with the current state is n based on the value of the third field included in the data structure of the current state, the acquisition unit 31 is used to read the second field included in the data structure of the previous state to obtain the n pattern string IDs directly associated with the current state. The judgment unit 32 is also used to perform the step of judging whether the first field included in the data structure of the current state is a non-zero value, where n is a positive integer greater than or equal to 1.

[0055] Optionally, if a state is the state corresponding to the last character of any preset string, then the third field of the data structure of the state is a non-zero value, and the second field of the data structure of the state stores the pattern string ID information associated with the preset string corresponding to the state. If a state is not the state corresponding to the last character of the preset string, then the third field of the data structure for that state will have a value of 0.

[0056] These units can be one or more integrated circuits configured to implement the above methods, such as one or more Application Specific Integrated Circuits (ASICs), one or more digital signal processors (DSPs), or one or more Field Programmable Gate Arrays (FPGAs). Alternatively, when one of these units is implemented using processing element scheduler code, the processing element can be a general-purpose processor, such as a Central Processing Unit (CPU) or other processor capable of calling program code. Furthermore, these units can be integrated together to form a system-on-a-chip (SOC).

[0057] Furthermore, regarding the string matching device provided in this application embodiment, from a hardware perspective, the hardware architecture diagram of the string matching device can be found in [reference needed]. Figure 4 As shown, the string matching device may include: a memory 40 and a processor 41. The memory 40 is used to store program instructions; the processor 41 calls the program instructions stored in the memory 40 and executes the above method embodiment according to the obtained program instructions. The specific implementation method and technical effect are similar, and will not be described again here.

[0058] Optionally, this application also provides a string matching device, including at least one processing element (or chip) for performing the above method embodiments.

[0059] Optionally, this application also provides a program product, such as a computer-readable storage medium storing computer-executable instructions for causing the computer to perform the above-described method embodiments.

[0060] Here, a machine-readable storage medium can be any electronic, magnetic, optical, or other physical storage device that can contain or store information, such as executable instructions, data, etc. For example, a machine-readable storage medium can be: RAM (Random Access Memory), volatile memory, non-volatile memory, flash memory, storage drives (such as hard disk drives), solid-state drives, any type of storage disk (such as optical discs, DVDs, etc.), or similar storage media, or combinations thereof.

[0061] The systems, devices, modules, or units described in the above embodiments can be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer, which can take the form of a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email sending and receiving device, game console, tablet computer, wearable device, or any combination of these devices.

[0062] For ease of description, the above devices are described separately by function as various units. Of course, in implementing this application, the functions of each unit can be implemented in one or more software and / or hardware.

[0063] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, embodiments of this application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0064] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0065] Furthermore, these computer program instructions can also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to operate in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in the process. Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0066] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0067] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.

Claims

1. A string matching method, characterized in that, The method includes: When constructing the AC state machine, a first field is defined in the data structure of each state to point to the index of the state containing the pattern string ID that is closest to that state on the Fail chain. When traversing the AC state machine based on the string to be matched, and traversing to the current state, the pattern string ID directly associated with the current state is obtained, and it is determined whether the first field included in the data structure of the current state is a non-zero value. If the first field of the data structure of the current state is determined to be a non-zero value, then based on the first field of the data structure of the current state, obtain the pattern string ID that is directly associated with the first state containing the pattern string ID on the Fail chain that is closest to the current state; and When it is determined that the first field of the data structure of the first state is a non-zero value, the pattern string ID directly associated with the second state containing the pattern string ID that is closest to the first state on the Fail chain is obtained, and it is determined whether the first field of the data structure of the second state is a non-zero value. The pattern string ID recording ends when it is determined that the first field of the data structure of a state is 0.

2. The method as described in claim 1, characterized in that, The data structure for a state also includes a second field for storing the pattern string ID information directly associated with that state; The steps for obtaining the pattern string ID directly associated with the current state include: Read the second field of the data structure of the current state; Based on the second field included in the data structure of the current state, obtain the pattern string ID directly associated with the current state.

3. The method as described in claim 2, characterized in that, If a state's data structure includes a second field used to directly store the pattern string ID directly associated with that state, then the step of obtaining the pattern string ID directly associated with the current state based on the second field included in the current state's data structure includes: The pattern string ID stored in the second field of the data structure of the current state is used as the pattern string ID directly associated with the current state; If a state's data structure includes a second field used to store a pointer to a pattern string ID directly associated with that state, stored independently, then the step of obtaining the pattern string ID directly associated with the current state based on the second field of the current state's data structure includes: Based on the second field of the data structure of the current state, a pointer to the independently stored pattern string ID directly associated with the current state is stored, and the pattern string ID directly associated with the current state is obtained.

4. The method as described in claim 2 or 3, characterized in that, A state's data structure also includes a third field for storing the number of pattern string IDs directly associated with that state; the method further includes: Read the third field included in the data structure of the current state; When the number of pattern string IDs directly associated with the current state is determined to be 0 based on the value of the third field included in the data structure of the current state, the step of determining whether the first field included in the data structure of the current state is a non-zero value is executed. When the number of pattern string IDs directly associated with the current state is determined to be n based on the value of the third field included in the data structure of the current state, the second field included in the data structure of the previous state is read to obtain the n pattern string IDs directly associated with the current state, where n is a positive integer greater than or equal to 1.

5. The method as described in claim 3, characterized in that, If a state is the state corresponding to the last character of any preset string, then the third field of the data structure of the state is a non-zero value, and the second field of the data structure of the state stores the pattern string ID information associated with the preset string corresponding to the state. If a state is not the state corresponding to the last character of the preset string, then the third field of the data structure for that state will have a value of 0.

6. A string matching device, characterized in that, The device includes: The building unit is used to define a first field in the data structure of each state when building the AC state machine, which is used to point to the index of the state containing the pattern string ID that is closest to the state on the Fail chain. The acquisition unit is used to acquire the pattern string ID directly associated with the current state when traversing the AC state machine based on the string to be matched and reaching the current state. The judgment unit is used to determine whether the first field included in the data structure of the current state is a non-zero value; If the determining unit determines that the first field included in the data structure of the current state is a non-zero value, the obtaining unit is further configured to, based on the first field included in the data structure of the current state, obtain the pattern string ID directly associated with the first state containing the pattern string ID on the Fail chain that is closest to the current state; and If the determination unit determines that the first field included in the data structure of the first state is a non-zero value, the acquisition unit is further configured to acquire the pattern string ID directly associated with the second state containing the pattern string ID that is closest to the first state on the Fail chain. The determination unit is further configured to determine whether the first field included in the data structure of the second state is a non-zero value, until the first field included in the data structure of a state is determined to be a zero value, and then the pattern string ID recording ends.

7. The apparatus as claimed in claim 6, characterized in that, The data structure for a state also includes a second field for storing the pattern string ID information directly associated with that state; When obtaining the pattern string ID directly associated with the current state, the obtaining unit is specifically used for: Read the second field of the data structure of the current state; Based on the second field included in the data structure of the current state, obtain the pattern string ID directly associated with the current state.

8. The apparatus as claimed in claim 7, characterized in that, If a state's data structure includes a second field used to directly store the pattern string ID directly associated with that state, then when obtaining the pattern string ID directly associated with the current state based on the second field included in the current state's data structure, the obtaining unit is specifically used for: The pattern string ID stored in the second field of the data structure of the current state is used as the pattern string ID directly associated with the current state; If a state's data structure includes a second field used to store a pointer to a pattern string ID directly associated with that state, stored independently, then when obtaining the pattern string ID directly associated with the current state based on the second field of the current state's data structure, the obtaining unit is specifically used for: Based on the second field of the data structure of the current state, a pointer to the independently stored pattern string ID directly associated with the current state is stored, and the pattern string ID directly associated with the current state is obtained.

9. The apparatus as claimed in claim 7 or 8, characterized in that, A state's data structure also includes a third field for storing the number of pattern string IDs directly associated with that state; The acquisition unit is further configured to read the third field included in the data structure of the current state; When the judgment unit determines that the number of pattern string IDs directly associated with the current state is 0 based on the value of the third field included in the data structure of the current state, it performs the step of judging whether the first field included in the data structure of the current state is a non-zero value. When the judging unit determines that the number of pattern string IDs directly associated with the current state is n based on the value of the third field included in the data structure of the current state, the obtaining unit is used to read the second field included in the data structure of the previous state to obtain the n pattern string IDs directly associated with the current state. The judging unit is also used to perform the step of judging whether the first field included in the data structure of the current state is a non-zero value, where n is a positive integer greater than or equal to 1.

10. The apparatus as claimed in claim 8, characterized in that, If a state is the state corresponding to the last character of any preset string, then the third field of the data structure of the state is a non-zero value, and the second field of the data structure of the state stores the pattern string ID information associated with the preset string corresponding to the state. If a state is not the state corresponding to the last character of the preset string, then the third field of the data structure for that state will have a value of 0.

11. A string matching device, characterized in that, The string matching device includes: Memory, used to store program instructions; A processor is configured to invoke program instructions stored in the memory and execute the steps of the method as described in any one of claims 1-5 according to the obtained program instructions.

12. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions for causing the computer to perform the steps of the method as described in any one of claims 1-5.