Regular expression synthesis method and device based on large language model demand understanding

By using a large language model for input completion and reflective iterative optimization, combined with multi-strategy local repair, the accuracy and stability issues in existing regular expression generation technologies are resolved, achieving automated synthesis of high-precision, high-coverage regular expressions.

CN120950416BActive Publication Date: 2026-04-07HUAQIAO UNIVERSITY
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-10-17
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

Existing regular expression generation technologies are insufficient in understanding natural language requirements, and lack accuracy and stability in generating results. Especially when dealing with complex business rules or high-precision matching requirements, they are difficult to achieve high coverage and accuracy, and lack effective feedback and iteration mechanisms.

Method used

By combining large language models for input completion, requirement understanding, and iterative optimization, a multi-strategy local repair method is adopted, including syntax tree splitting, model semantic splitting, reorganization repair, and single-point repair, to generate high-precision and high-coverage regular expressions.

Benefits of technology

It achieves automated synthesis of regular expressions with high precision and high coverage, significantly reducing the cost of manual intervention, improving the accuracy and robustness of the generated results, and possessing the ability for autonomous diagnosis and progressive optimization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120950416B_ABST
    Figure CN120950416B_ABST
Patent Text Reader

Abstract

The application discloses a regular expression synthesis method and device based on large language model demand understanding, relates to the technical field of software testing, and comprises the following steps: completing and refining a natural language description input by a user and a regular use case through a large language model, generating a more comprehensive regular use case; constructing a demand understanding prompt word to generate a candidate structured understanding, and screening an optimal understanding through scoring; combining demand and understanding to generate a candidate regular expression, and optimizing by reflection iteration; for a use case that does not pass, introducing a local repair module, splitting a regular expression into a syntax tree or a pattern, positioning an error sub-mode, and accurately repairing by using various repair strategies. Through the demand understanding, reflection, iterative optimization and local sub-mode repair technology of the large language model, the application realizes high precision and strong robustness of regular expression generation.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of software testing, in particular to a regular expression synthesis method and device based on large language model requirement understanding. BACKGROUND

[0002] As a high-efficiency text pattern matching tool, regular expression (Regex) is widely used in data verification, information extraction, log analysis, data cleaning, network security and other fields. The traditional writing of regular expression depends on the professional experience of the developer and the understanding of the target data pattern, and the design and debugging process is time-consuming and prone to errors, especially when complex business rules, large character sets or high-precision matching requirements are involved, the difficulty of manual writing increases significantly.

[0003] Existing automatic regular generation techniques mainly include template matching, program induction and deep learning-based methods. For example, the RegexGen method based on DSL (Domain Specific Language) can generate regular expressions given positive and negative example cases; the Seq2Regex method based on neural network attempts to directly map natural language description to regular expression, realizing end-to-end automatic generation. However, these methods still have the following shortcomings: such as insufficient semantic understanding of natural language requirements, the generated results may not meet all positive and negative example cases; lack of effective feedback iteration mechanism, generated errors are difficult to correct gradually;

[0004] When the regular expression is long or complex, local errors are difficult to locate and repair accurately; when only part of the information is provided (such as only natural language description or only regular example), the generation accuracy decreases significantly.

[0005] In recent years, large language models (LLM) have made significant progress in natural language understanding and code generation, but directly applying LLM to regular synthesis still faces the problem of insufficient accuracy and stability. Therefore, there is an urgent need for a regular synthesis method that combines input completion, requirement understanding, reflection mechanism and local repair strategy to improve the accuracy, robustness and adaptability of the generated results. SUMMARY

[0006] To solve the above problems, the present application proposes a regular expression synthesis method and device based on large language model requirement understanding, which realizes high-precision, high-coverage and iterative optimization of regular expression automatic synthesis by combining input completion, understanding and reflection iteration optimization and multi-strategy local repair technical means, significantly reduces the cost of manual intervention and improves the accuracy and robustness of the generated results.

[0007] The specific scheme is as follows:

[0008] In one aspect, the regular expression synthesis method based on large language model requirement understanding includes:

[0009] S1, obtaining a natural language description input by a user and a regular case set; when one of the natural language description or the regular case set is missing, calling a large language model to complete the missing information, and then calling the large language model again to refine the natural language description and the regular case set, and outputting the refined natural language description and the regular case set;

[0010] S2, constructing a requirement understanding prompt word based on the refined natural language description and the regular case set, and inputting the large language model to generate a plurality of candidate understandings, scoring the matching performance of each candidate understanding on the regular case set by a scorer, and screening an optimal understanding;

[0011] S3, inputting the optimal understanding, the refined natural language description and the regular case set into the large language model to generate a candidate regular expression, performing positive and negative example verification on the candidate regular expression, and outputting a failed candidate regular expression and a failed regular case;

[0012] S4, constructing a reflection prompt word based on the failed candidate regular expression and the failed regular case, and inputting the large language model to generate a plurality of candidate reflections; scoring the consistency performance of each candidate reflection on the failed regular case by a scorer, and screening an optimal reflection;

[0013] S5, inputting the optimal reflection, the failed candidate regular expression and the failed regular case into the large language model for repair, generating a repaired regular expression, and performing positive and negative example verification, and outputting a regular case that still fails;

[0014] S6, determining whether there is a regular case that still fails, if so, starting a local repair module, performing syntax tree splitting or model semantic splitting on the current regular expression to obtain a sub-pattern list; by replacing each item in the sub-pattern list with a wildcard, determining whether the positive example passes, thereby locating the error sub-pattern, and using a recombination repair, single-point repair, adjacent window repair, non-adjacent merge block repair or multi-point independent repair strategy, calling a sub-pattern refinement module to accurately repair the local sub-pattern, generating a repaired regular expression and performing overall verification, until all regular cases or the maximum repair number is reached, and confirming that the regular expression synthesis is completed.

[0015] Further, in S1, a large language model is called to refine the natural language description and the regular use case set, and outputs the refined natural language description and the regular use case set, specifically including: based on the completed natural language description and the regular use case set, the large language model actively identifies and proposes potential boundary conditions, format constraints and implicit semantic assumptions, generates extended positive examples and negative examples, to eliminate requirement ambiguity and improve the integrity and consistency of input information; and supports user operations of adding, deleting and modifying the completed and generated content in a unified interactive interface, and finally outputs the refined natural language description and the regular use case set.

[0016] Further, in S2, the requirement understanding prompt word includes at least one of a format requirement, a field range, a boundary condition or a special rule, for guiding the large language model to generate a structured understanding.

[0017] Further, in S2, the scorer takes the matching degree of the candidate understanding on the regular use case as an evaluation index, scores and sorts each candidate understanding, and selects the optimal understanding in the current round; if the optimal understanding in the current round still fails to pass all regular use cases, the large language model is driven to iteratively generate a new round of candidate understanding, and the scoring and screening process is repeated until the generated understanding meets all regular use cases or reaches a preset maximum iteration number, and finally the optimal understanding is output.

[0018] Further, in S4, the reflection prompt word includes a failed candidate regular expression, a type of failed use case and a regular use case of the failed use case, the large language model analyzes the reasons for failure to obtain the corresponding correction logic, and then the scorer takes the consistency score between the corresponding correction logic and the failed use case as the core evaluation index, scores and sorts each candidate reflection, and selects the optimal reflection in the current round; if the optimal reflection in the current round still fails to pass all regular use cases, the large language model is driven to iteratively generate a new round of candidate reflection, and the evaluation and screening process is repeated until all regular use cases are met or a preset maximum iteration number is reached, and finally the optimal reflection is output.

[0019] Further, in S6, the syntax tree splitting specifically includes: generating an abstract syntax tree through a regular syntax parser, and extracting each node into an independent sub-pattern; the model semantic splitting specifically includes: calling a large language model to split the regular expression according to semantic boundaries, and verifying whether the recombination of the split sub-patterns is equivalent to the original regular expression to obtain a sub-pattern list.

[0020] Further, in S6, it is judged whether the positive examples pass by replacing each item in the sub-pattern list with a wildcard, so as to locate the error sub-pattern, specifically comprising: performing splitting on the candidate regular expression to obtain a sub-pattern list; replacing one or more sub-patterns in the sub-pattern list with a wildcard to form a temporary regular expression; if the temporary regular expression can pass all regular positive examples, it is determined that there is a repairable error sub-pattern or error combination in the replaced region.

[0021] Further, in S6, the recombination repair includes sequential rearrangement, enumeration deletion and enumeration insertion.

[0022] The sequential rearrangement is specifically: performing full permutation combination on the sub-patterns, and verifying whether the new sequence passes all regular use cases.

[0023] The enumeration deletion is specifically: attempting to delete each sub-pattern, and verifying whether the remaining combination passes all regular use cases, if yes, determining that the deleted sub-pattern is redundant.

[0024] The enumeration insertion is specifically: enumerating insertion of a specified wildcard between sub-patterns, and calling a single-point or multi-point repair module to fine-tune the wildcard region.

[0025] On the other hand, a regular expression synthesis device based on large language model demand understanding comprises:

[0026] A refining module is configured to obtain a natural language description input by a user and a regular use case set; when one of the natural language description or the regular use case set is missing, a large language model is called to complete the missing information, and then the large language model is called again to refine the natural language description and the regular use case set, and output the refined natural language description and regular use case set.

[0027] An optimal understanding screening module is configured to construct a demand understanding prompt word based on the refined natural language description and the regular use case set, and input the large language model to generate a plurality of candidate understandings, score the matching performance of each candidate understanding on the regular use case through a scorer, and screen out an optimal understanding.

[0028] A verification module is configured to input the optimal understanding, the refined natural language description and the regular use case set into the large language model to generate a candidate regular expression, perform positive and negative example verification on the candidate regular expression, and output a candidate regular expression that does not pass and a regular use case that does not pass.

[0029] An optimal reflection screening module is configured to construct a reflection prompt word based on the candidate regular expression that does not pass and the regular use case that does not pass, and input the large language model to generate a plurality of candidate reflections; score the consistency performance of each candidate reflection on the regular use case that does not pass through a scorer, and screen out an optimal reflection.

[0030] a repair module for inputting the optimal reflection, the failed candidate regular expression and the failed regular case into a large language model for repair, generating a repaired regular expression, and performing positive and negative example verification to output regular cases that still fail;

[0031] a synthesis module for determining whether there are regular cases that still fail, and if so, starting a local repair module to perform syntax tree splitting or model semantic splitting on the current regular expression to obtain a sub-pattern list; by replacing each item in the sub-pattern list with a wildcard, determining whether the positive example passes, thereby locating the error sub-pattern, and using a recombination repair, single-point repair, adjacent window repair, non-adjacent merge block repair or multi-point independent repair strategy to call a sub-pattern refinement module to accurately repair the local sub-pattern, generate a repaired regular expression and perform overall verification until all regular cases or the maximum number of repairs is reached, and confirm that the regular expression synthesis is complete.

[0032] The present application adopts the above technical solution and has beneficial effects:

[0033] (1) The present application realizes the ability of the large language model to extract structured semantic rules from fuzzy natural language descriptions by constructing requirement understanding cues containing format requirements, field ranges and boundary conditions, and using a scorer to quantitatively score and filter the matching degree of multiple candidate understandings on regular cases.

[0034] (2) The present application realizes accurate positioning and safe repair of local errors in complex regular expressions by generating an abstract syntax tree using a regular grammar parser for syntax tree splitting, or calling a large language model for semantic boundary-based model semantic splitting to obtain a list of independently verifiable sub-patterns, and after locating error sub-patterns in combination with regular positive examples, enabling recombination repair, single-point repair or multi-point independent repair strategies to call a sub-pattern refinement module for local correction.

[0035] (3) The present application realizes the ability of the system to autonomously diagnose and gradually optimize after the first generation of failure by constructing reflection cues based on failed candidate regular expressions and their failure cases, driving the large language model to analyze error causes and generate correction logic, and then using a scorer to sort and filter based on the consistency of the correction logic and the failed cases as the core indicator, forming a reflection-optimization closed loop of multiple iterations, effectively addressing the challenges of regular synthesis in complex situations such as boundary scenarios and implicit constraints. BRIEF DESCRIPTION OF DRAWINGS

[0036] Figure 1 Flowchart of the regular expression synthesis method based on large language model requirement understanding of the embodiments of the present application;

[0037] Figure 2This is a schematic diagram of the overall structure of an embodiment of the present invention;

[0038] Figure 3 This is a structural schematic diagram for understanding and reflection on embodiments of the present invention;

[0039] Figure 4 This is a diagram of a regular expression synthesis device based on large language model requirement understanding, according to an embodiment of the present invention. Detailed Implementation

[0040] The present invention will be further described in detail below with reference to the embodiments and accompanying drawings, but the embodiments of the present invention are not limited thereto.

[0041] like Figure 1 As shown, the present invention relates to regular expression synthesis based on large language model requirements understanding, including:

[0042] S1: Obtain the natural language description and regular expression test case set input by the user; when either the natural language description or the regular expression test case set is missing, call the large language model to complete the missing information, and then call the large language model again to refine the natural language description and regular expression test case set, and output the refined natural language description and regular expression test case set.

[0043] Specifically, the large language model is invoked to refine the natural language description and regular expression test case set, outputting a refined natural language description and regular expression test case set. This includes: the large language model actively identifying and proposing potential boundary conditions, format constraints, and implicit semantic assumptions based on the completed natural language description and regular expression test case set, generating extended positive and negative examples to eliminate requirement ambiguity and improve the completeness and consistency of input information; and supporting users to add, delete, and modify the completed and generated content in a unified interactive interface, ultimately outputting the refined natural language description and regular expression test case set.

[0044] Specifically, the system obtains the initial natural language description and / or regular expression test cases input by the user. For example, if the user inputs the initial natural language description: "Extract dates in the form of 2023-08-14, but do not match illegal dates such as 'abc' or '99 / 99 / 9999'", the system detects a missing set of regular expression test cases and calls the Large Language Model (LLM) to generate an initial set of test case suggestions (e.g., positive examples: "2023-08-14", "1902-11-01"; negative examples: "abc", "99 / 99 / 9999", "2023-13-45", etc.), and displays them back to the user for adjustment and confirmation. After user confirmation, the system sends the final description and test cases back to the LLM to generate a more accurate natural language description and more comprehensive regular expression test cases, and finally displays them back to the user for a second review and lock.

[0045] S2, based on the refined natural language description and the regular use case set, a requirement understanding prompt word is constructed, and a large language model is input to generate a plurality of candidate understandings, a scorer is used to score the matching performance of each candidate understanding on the regular use case, and the optimal understanding is screened out.

[0046] Specifically, the requirement understanding prompt word includes at least one of format requirements, field ranges, boundary conditions or special rules, and is used to guide the large language model to generate a structured understanding.

[0047] Specifically, the scorer takes the matching degree of the candidate understanding on the regular use case as an evaluation index, scores and sorts each candidate understanding, and screens out the optimal understanding of the current round; if the optimal understanding of the current round still fails to pass all regular use cases, the large language model is driven to iteratively generate a new round of candidate understandings, and the scoring and screening process is repeated until the generated understanding meets all regular use cases or reaches a preset maximum iteration number, and finally the optimal understanding is output.

[0048] Specifically, the understanding prompt is to splice the natural language description and positive and negative examples into a requirement understanding prompt word Prompt0, requiring the large model to output an understanding containing format requirements, field ranges, leap year rules, boundary conditions and other constraints of regular expressions; candidate understanding generation and scoring is that the LLM outputs N candidate understandings {U_i}; the scorer scores the consistency and coverage of {U_i} on the use case set, and if the highest score does not reach the threshold, the iteration prompt (including error guidance and missing points) is performed, until the best understanding U0 is obtained or the maximum iteration number is reached. Then, the understanding U0 is combined with the task input to generate candidate regular expressions {R_i}, which are quickly verified on the use case set, and the R0 with the highest score is retained.

[0049] S3, input the optimal understanding, the refined natural language description and the regular use case set into the large language model to generate candidate regular expressions, perform positive and negative example verification on the candidate regular expressions, and output the candidate regular expressions that fail and the regular use cases that fail.

[0050] S4, based on the failed candidate regular expressions and the failed regular use cases, a reflection prompt word is constructed, and a large language model is input to generate a plurality of candidate reflections; a scorer is used to score the consistency performance of each candidate reflection on the failed regular use cases, and the optimal reflection is screened out.

[0051] Specifically, the reflection prompt words include a failed candidate regular expression, a type of failed use case, and a regular use case of the failed use case, a reason for failure is analyzed by the large language model, corresponding correction logic is obtained, then a consistency score between the corresponding correction logic and the failed use case is taken as a core evaluation index by the scorer, each candidate reflection is scored and sorted, and the optimal reflection of the current round is selected; if the optimal reflection of the current round still fails to pass all regular use cases, the large language model is driven to iteratively generate a new round of candidate reflections, and the evaluation and selection process is repeated until all regular use cases are met or a preset maximum iteration number is reached, and finally the optimal reflection is output.

[0052] Specifically, in the reflection process, if R0 still has a failed sample, the system constructs a reflection prompt Prompt1 based on the failed sample, and requires the large model to output an understanding that contains the constraints of the regular expression (such as field range, leap year rule, boundary condition, etc.); then the scorer scores the consistency and coverage of {I_i} on the failed use case set, and if the highest score does not reach the threshold, the iteration is prompted (including error guidance and missing points), until the best reflection I or the maximum iteration number is reached.

[0053] S5, input the optimal reflection, the current regular expression and the failed regular use case into the large language model for repair, generate a repaired regular expression, and perform positive and negative example verification to output regular use cases that still fail.

[0054] S6, judge whether there is a regular use case that still fails, if yes, start the local repair module, split the current regular expression by syntax tree or model semantics to obtain a sub-pattern list; by replacing each item in the sub-pattern list with a wildcard, judge whether the positive example passes, so as to locate the error sub-pattern, and adopt the recombination repair, single-point repair, adjacent window repair, non-adjacent merge block repair or multi-point independent repair strategy, call the sub-pattern fine repair module to accurately repair the local sub-pattern, generate a repaired regular expression and perform overall verification, until all regular use cases or the maximum repair number is reached, and the regular expression synthesis is completed.

[0055] Specifically, the syntax tree splitting is specifically: generating an abstract syntax tree through a regular syntax parser, and extracting each node into an independent sub-pattern; the model semantic splitting is specifically: calling the large language model to split the regular expression according to the semantic boundary, and verifying whether the recombination of the split sub-pattern is equivalent to the original regular expression to obtain a sub-pattern list; the recombination repair includes sequential rearrangement, enumeration deletion and enumeration insertion;

[0056] The sequential rearrangement is specifically: performing full permutation and combination on the sub-patterns, and verifying whether the new sequence passes all regular use cases;

[0057] The enumeration deletion specifically refers to sequentially attempting to delete each sub-pattern and verifying whether the remaining combination passes all regular cases, and if so, determining that the deleted sub-pattern is redundant;

[0058] Specifically, in the specific local repair stage, the embodiment adopts various fine strategies to accurately repair each sub-pattern. First, in the reordering repair, the system optimizes the structure through three ways: one is sequential rearrangement, that is, performing full permutation and combination on the sub-patterns of the regular expression to be repaired, generating a new sequence and performing positive and negative example verification, if it can completely match all positive examples and exclude all negative examples, the repair is completed; two is enumeration deletion, for the sub-patterns that may exist redundancies, the system sequentially removes each sub-pattern, and verifies the remaining part after recombination, if it can pass the test after deleting a sub-pattern, it is determined that it is a redundant item and is removed; three is enumeration insertion, for the positions that may be missing key sub-patterns, the system sequentially inserts a general capture pattern (?P <lang>*), and after verifying the repairable possibility in the regular positive example, a single-point or multi-point repair module is called to further refine the wildcard area.

[0059] Secondly, single-point repair makes accurate correction to the regular expression area to be repaired, and replaces the sub-pattern with (?P <lang>.*) After that, the sub-pattern repair module repair2one is called, which receives the current replaced sub-pattern, the list of all positive and negative example blocks, and the failed use case, generates candidate repair solutions using a large language model, and ensures that the repaired sub-pattern can match all positive example blocks and exclude all negative example blocks through iterative verification, until success or reaching the maximum number of attempts. When single-point repair fails, the system initiates single-point adjacent window repair (adjacent_window_repair) to expand several adjacent sub-patterns to the left and right of the error position to form a joint repair block, and replace the entire block with (?P <lang>*), and then the repair2one module is used for overall fine-tuning, which is suitable for processing problems caused by multiple adjacent sub-patterns cooperating errors. To further deal with complex errors across regions, the system introduces a non-adjacent merged block repair (merged_block_repair), selects the positions of the front end and the last end in multiple discontinuous but semantically related error sub-patterns as boundaries, and unifies all sub-patterns in the interval into an overall repair block, which is uniformly replaced by (?P <lang>.*) After calling repair2one for integrated repair, the scattered but logically dependent errors are effectively solved, and the coherence and efficiency of repair are improved. In addition, non-adjacent multi-repair (non_adjacent_multi_repair) separately performs independent replacement and repair on multiple discontinuous error sub-patterns, each of which is guaranteed to be correct by the repair2one module, avoiding the introduction of positive and negative example conflicts during repair. Finally, all repaired sub-patterns are recombined to form a complete regular expression. Through the above multi-level, multi-strategy collaborative local repair mechanism, the system realizes high-precision positioning and safe correction of various structural errors in complex regular expressions, significantly improving the repair success rate and generation quality.

[0060] Specifically, the local repair (single_point_repair) system splits the regular to be repaired (e.g., the date "year-month-day" three segments): syntax tree splitting: taking the regular expression as input, the syntax parser parses the regular expression into an abstract syntax tree including character nodes, character class nodes, connection nodes, selection nodes, repetition nodes, grouping nodes, and quantifier nodes, etc.; performing depth-first traversal on the abstract syntax tree to extract each node into an independent sub-pattern; outputting each independent sub-pattern obtained by splitting in order to form a list of split sub-patterns. Model semantic splitting: requires the LLM to split the regular expression according to semantic boundaries, and after splitting, it needs to compare the split sub-pattern list with the original list to check for consistency. Sub-pattern recombination repair specifically refers to different permutations and combinations of the sub-patterns of the regular expression to be repaired to form new sub-pattern sequences; for sub-patterns that may have redundancies, the system removes each sub-pattern in turn and arranges the remaining sub-patterns to form new sub-pattern sequences; combine all new sub-pattern sequences for positive and negative example verification; if the rearranged sub-patterns can match all positive examples and exclude negative examples, the repair is complete; taking the sub-pattern list as input, let the list length be n, there are n+1 possible insertion positions, where the 0th position is before the first sub-pattern, the ith position is after the ith sub-pattern, 1≤i≤n; for positions that may be missing sub-patterns, the system inserts a universal capture pattern (?P <lang>.*) The candidate regular expressions are output in sequence to form a complete regular expression set after wildcard insertion; and a single-point repair or multi-point repair module is called to refine the wildcard area; the single-point replacement and verification part temporarily replaces p_i with a wildcard capture (?P <lang>repair2one fine-tuning part calls the sub-pattern fine-tuning module repair2one for p_i determined to be repairable: input current sub-pattern p_i, positive example capture value and negative example capture value list, failure sample capture value list; construct a prompt containing "unmatched positive example / mismatched negative example", call LLM to output the repaired sub-pattern p_i'; iterate p_i' to pass or reach the maximum number of times; output the compliant repaired sub-pattern p_i'; backfill and overall validation part backfill the original position with p_i' and overall validation; if passed, end, otherwise proceed to multi-point repair.

[0061] Specifically, in the local repair stage, the system implements a multi-strategy collaborative fine-tuning repair mechanism for different types of regular expression errors. When single-point repair fails and the error involves joint logic problems between adjacent sub-patterns, the system starts single-point adjacent window repair (adjacent_window_repair):

[0062] First, locate the sub-pattern where the mismatch occurs on the negative example (such as regular R2 = (?:https?: / / )?(?:www\.)?([a-z0-9-]+)(\.[a-z]{2,})+ mismatches on the negative example "https: / / 123-.com", locate ([a-z0-9-]+) as the suspected error area), take this position as the center, expand to the left and right by a preset window width w, form a repair block containing the context (such as (?:www\.)?([a-z0-9-]+)(\.[a-z]{2,}) ); replace the repair block as a wildcard capture pattern (?P <lang>.*) Construct temporary regular R_tmp for repairability verification; if the verification is passed, the original repair block text is called as input to the sub-pattern fine-tuning module repair2one to generate the repaired sub-pattern B', which is then backfilled into the original expression for overall verification. If the repair is not successful, the system will adaptively adjust the window size w and try an asymmetric expansion strategy (such as only left expansion or only right expansion) until the repair is successful or the next repair strategy is entered. For cases where errors are distributed in multiple discontinuous positions but there is semantic dependence, the system uses non-adjacent merged block repair (merged_block_repair): the smallest index and the largest index in all error positions are selected as the start and end boundaries of the repair interval, and all sub-patterns in the interval are merged into a continuous whole repair block, and (?P <lang>.*) After replacement, verify its repairability; if the conditions are met, input the original text of the merged block into the repair2one module for integrated repair, and return the repair result and perform overall verification. This strategy effectively solves the logical coupling problem between cross-region errors through the "start-stop surrounding, middle segment merging" method, avoiding mutual interference caused by multiple independent repairs. For multiple discontinuous and independent error sub-patterns (such as "(cat|dog)s?" and "pet" in R4 = (cat|dog)s?|pet), the system performs non-adjacent multi-repair (non_adjacent_multi_repair): replace each error sub-pattern with an independent wildcard (?P<lang_k>.*), verify whether R_tmp after replacement can still match all positive examples, then call the repair2one module to perform independent repair on each replaced sub-pattern, obtain the repaired sub-pattern set {p_k'}, and finally combine {p_k'} into a complete regular expression in the original order and perform overall verification until all test cases pass or the maximum number of iterations is reached. The core execution unit of the above repair process is the sub-pattern repair module repair2one, which receives the sub-pattern to be repaired sub_re, the capture value set of positive and negative examples, and the list of regular test cases that have not passed. In the initialization stage, it first verifies whether the current sub-pattern meets the matching requirements, and if so, it returns directly; otherwise, it constructs a prompt word containing un-matched positive examples, mis-matched negative examples, and context constraints, calls a large language model to generate repair candidates sub_re', and continuously optimizes through iterative verification until the generated result correctly matches all positive examples and excludes all negative examples, or reaches the maximum number of attempts. For complex regular expressions containing explicit branch structure "|", the system enables the branch refactoring module (branch_refactor_repair) for special processing: first, identify the outermost "| " operator through the syntax parser to split the expression into multiple independent branches {b1, b2, …, bn}; then perform full positive and negative example verification on each branch bi, calculate its matched positive example set P(bi) and mis-matched negative example set N(bi), and mark the negative pollution branch (N(bi) is not empty) and the invalid branch (P(bi) is empty); keep the top k valid branches that are neither polluted nor invalid and cover the most positive examples (k is determined by a pre-set threshold or heuristic rules); for the un-covered positive examples and all negative examples, generate sub-problems to recursively perform branch splitting and verification processes; finally, de-duplicate and merge all valid branches, use "|" to re-assemble them into a new branch structure and return to the upper-level strategy for backfilling and overall checking.Through the cooperation of the above multi-granularity and multi-path local repair mechanism, the application can generate high-precision and high-coverage regular expressions from the natural language description and regular use cases input by the user, and guarantee the correctness and robustness in positive and negative example verification through reflection guidance and multi-level repair strategy, significantly reduce the cost of manual intervention, and improve the automation level and overall efficiency of regular expression generation and optimization.

[0063] Specifically, Figure 2 An overall structure diagram of a regular expression synthesis method based on large language model requirement understanding is shown: first, the user inputs natural language description and regular use case set, and the large language model (LLM) is used to complete the missing information and refine the input; then, the initial regular expression is generated based on the refined input and verified, if not passed, the requirement understanding prompt word is constructed to guide the large model to generate multiple candidate understandings, and the best understanding is selected by the scorer; then, the best understanding is used to generate a new regular expression for verification, if it still fails, the reflection prompt word is constructed to analyze the failure reason, generate reflection and further optimize the regular expression; finally, the regular use cases that still fail are adjusted by splitting sub-patterns, reorganizing repair, single-point repair or multi-point repair, etc., until all use cases pass or reach the maximum repair times, so as to realize accurate and efficient regular expression synthesis.

[0064] Specifically, Figure 3 The iterative optimization process of understanding and reflection in the regular expression generation process is shown, the "understanding" part on the left side starts from the natural language description and regular use cases, generates and stores the understanding through the large model, and then the scorer is used for verification; if not passed, the information is fed back to optimize the understanding, until the iteration termination condition is met or the best understanding is generated, and further generate the regular expression for verification. The "reflection" part on the right side is aimed at the regular expression that fails to pass the verification, and the condition is evaluated to determine whether reflection needs to be generated; if so, record and generate reflection, and then generate a new regular expression for verification, until the final regular expression is output after passing, otherwise continue to feed back information to optimize reflection, forming a closed-loop iterative optimization mechanism.

[0065] Specifically, in this embodiment, the method is applied specifically. First, the natural language description and / or regular use case input by the user are obtained; when one of the information is missing, the large language model is called to complete the context and echo, and the user adds, deletes or modifies in the unified interface. After the user confirms, the system sends the description and use case to the large model again, refines a more accurate natural language description and expands a more comprehensive regular use case, and finally echoes the user for secondary review and locking, and after completion, enters the next step. Second, based on the natural language description and regular use case, a requirement understanding prompt word is constructed, which is input into the large language model to generate a structured understanding; the optimal understanding is selected through a scorer until all use cases are met or the maximum number of iterations is reached; then the optimal understanding and the requirement are input into the large language model to generate a regular expression; the generated result is verified by positive and negative examples and the failed use cases are output; then based on the failed use cases, a reflection prompt word is constructed, which is input into the large language model to generate a reflection, and the optimal reflection is selected through a scorer until all failed use cases are met or the maximum number of iterations is reached; then the optimal reflection and the incorrect regular expression, the failed regular use case, etc. are input into the large language model to repair the regular expression; the repair result is verified by positive and negative examples and the failed use cases are output; if there are still failed use cases, the reflection repair can be repeated, or the local repair is entered. Finally, when the generated regular expression still fails to pass all regular use cases, the system starts the local repair module, and the regular expression to be repaired is first split according to the syntax tree or based on the semantic splitting of the large language model. Then the local repair stage is performed.

[0066] Specifically, in this specification, "understanding, reflection" refers to the structured constraint description of the target matching task (such as field range, format, boundary condition, etc.); "positive and negative example use cases" respectively refer to the expected matching / expected non-matching string set; "sub-pattern" refers to the small functional unit or logical block after semantic division of the regular expression; "scorer" refers to an evaluation module for measuring the performance of candidate understanding and reflection on the use case set. In each stage of the present application, unless otherwise specified, the passing standard of the regular expression or sub-pattern at least meets: (a) all positive examples should be "fully matched"; (b) all negative examples should be "fully rejected".

[0067] Overall, the present application can automatically complete and refine input information using large language models to generate structured requirement descriptions and regular case sets in the case of incomplete natural language descriptions or missing regular use cases provided by the user, and ensure the completeness and accuracy of the input content through echo display and user interaction confirmation. Compared with the traditional method of completely relying on manual writing and repeated adjustment, the present application significantly reduces the user's threshold and technical requirements, improves the initial input quality, and lays a reliable foundation for subsequent high-precision regular generation. On this basis, a double-layer optimization mechanism combining understanding and reflection is proposed: in the generation stage, the large language model is guided to generate candidate regular expressions with clear semantics and reasonable structure by constructing requirement understanding prompts containing format constraints, field ranges, boundary conditions, etc.; after verification fails, reflection is constructed based on the failed use cases to drive the model to analyze the error causes and generate correction logic. This process quantitatively evaluates and iteratively selects each candidate reflection through a scorer, forming a closed-loop process of "generation-verification-reflection-optimization", which realizes the continuous evolution and self-correction ability of regular expressions, effectively overcoming the problem of low one-time generation accuracy and difficulty in convergence, and significantly reducing the cost of manual intervention and repeated debugging. A multi-strategy collaborative local repair method is designed, covering a variety of fine-grained repair strategies such as recombination repair, single-point repair, adjacent window repair, non-adjacent merged block repair, non-adjacent multi-point independent repair, and branch reconstruction. The system first splits the regular expression to be repaired into sub-pattern units, accurately locates the error area, and adaptively selects the optimal repair path according to the error type, adjusting only the local part while preserving the correct structure. Among them, the repair2one module is responsible for fine-grained sub-pattern repair, and the branch_refactor module is specifically designed to handle complex logic with "| " branch structure, and the two modules work together to achieve comprehensive coverage from simple patterns to complex structures. This local repair mechanism avoids the uncertainty brought by whole rewriting, significantly improves repair efficiency, matching accuracy, and negative example exclusion ability, and finally achieves the goal of high-precision, low-disturbance, and iterative regular automatic synthesis.

[0068] As Figure 4 shown, the present embodiment also discloses a regular expression synthesis device based on large language model requirement understanding, comprising:

[0069] The refining module 41 is configured to obtain the natural language description and the regular case set input by the user; when one of the natural language description or the regular case set is missing, the large language model is called to complete the missing information, and then the large language model is called again to refine the natural language description and the regular case set, and output the refined natural language description and the refined regular case set.

[0070] An optimal understanding screening module 42 is configured to construct a requirement understanding prompt word based on the refined natural language description and the regular use case set, input a large language model to generate a plurality of candidate understandings, score the matching performance of each candidate understanding on the regular use case by a scorer, and screen out an optimal understanding.

[0071] A verification module 43 is configured to input the optimal understanding, the refined natural language description and the regular use case set into the large language model to generate a candidate regular expression, perform positive and negative example verification on the candidate regular expression, and output a failed candidate regular expression and a failed regular use case.

[0072] An optimal reflection screening module 44 is configured to construct a reflection prompt word based on the failed candidate regular expression and the failed regular use case, input a large language model to generate a plurality of candidate reflections, score the consistency performance of each candidate reflection on the failed regular use case by a scorer, and screen out an optimal reflection.

[0073] A repair module 45 is configured to input the optimal reflection, the current regular expression and the failed regular use case into the large language model for repair, generate a repaired regular expression, and perform positive and negative example verification to output a regular use case that still fails.

[0074] A synthesis module 46 is configured to determine whether there is a regular use case that still fails, and if so, start a local repair module, perform syntax tree splitting or model semantic splitting on the current regular expression to obtain a sub-mode list, locate an error sub-mode based on the sub-mode list, and adopt a recombination repair, single-point repair, adjacent window repair, non-adjacent merge block repair or multi-point independent repair strategy to call a sub-mode fine repair module to accurately repair the local sub-mode, generate a repaired regular expression and perform overall verification until all regular use cases are passed or the maximum repair number is reached, and confirm that the regular expression synthesis is completed.

[0075] The specific implementation of the regular expression synthesis system based on the requirement understanding of the large language model is the same as the regular expression synthesis method based on the requirement understanding of the large language model, and the present embodiment will not be repeated.

[0076] Although the present application is specifically shown and introduced in combination with the preferred embodiments, it should be understood by those skilled in the art that various changes can be made in form and details without departing from the spirit and scope of the present application defined in the appended claims, and all such changes are within the protection scope of the present application.< / lang> < / lang> < / lang> < / lang> < / lang> < / lang> < / lang> < / lang>

Claims

1. A regular expression synthesis method based on large language model requirement understanding, characterized in that, Includes the following steps: S1: Obtain the natural language description and regular expression test case set input by the user; when either the natural language description or the regular expression test case set is missing, call the large language model to complete the missing information, and then call the large language model again to refine the natural language description and regular expression test case set, and output the refined natural language description and regular expression test case set. S2: Based on the refined natural language description and regular use case set, construct requirement understanding prompt words, and input them into the large language model to generate multiple candidate understandings. The scorer scores the matching performance of each candidate understanding on the regular use case and selects the optimal understanding. S3 inputs the optimally understood and refined natural language description and regular expression test case set into the large language model to generate candidate regular expressions, performs positive and negative example verification on the candidate regular expressions, and outputs the candidate regular expressions and the regular expression test cases that fail. S4: Construct reflection prompts based on failed candidate regular expressions and failed regular expression use cases, and input them into a large language model to generate multiple candidate reflections; use a scorer to score the consistency performance of each candidate reflection on failed regular expression use cases, and select the optimal reflection; S5 inputs the optimal reflection, the failed candidate regular expressions and the failed regular expression test cases into the large language model for repair, generates the repaired regular expressions, performs positive and negative example verification, and outputs the regular expression test cases that still fail. S6. Determine if there are still any regular expression test cases that fail. If so, start the local repair module to split the current regular expression into a syntax tree or model semantics to obtain a sub-pattern list. By replacing each item in the sub-pattern list with a wildcard, determine if the positive example passes, thereby locating the erroneous sub-pattern. Then, use reorganization repair, single-point repair, adjacent window repair, non-adjacent merge block repair, or multi-point independent repair strategies to call the sub-pattern refinement module to accurately repair the local sub-pattern, generate the repaired regular expression, and perform overall verification until all regular expression test cases or the maximum number of repairs is reached, confirming that the regular expression synthesis is complete.

2. The regular expression synthesis method based on large language model requirement understanding according to claim 1, characterized in that, In S1, the large language model is invoked to refine the natural language description and regular expression test case set, outputting the refined natural language description and regular expression test case set. Specifically, the large language model actively identifies and proposes potential boundary conditions, format constraints, and implicit semantic assumptions based on the completed natural language description and regular expression test case set, generating extended positive and negative examples to eliminate requirement ambiguity and improve the completeness and consistency of input information; and supports users to add, delete, and modify the completed and generated content in a unified interactive interface, finally outputting the refined natural language description and regular expression test case set.

3. The regular expression synthesis method based on large language model requirement understanding according to claim 1, characterized in that, In S2, the requirement understanding prompts include at least one of the following: format requirements, field ranges, or boundary conditions, which are used to guide the large language model to generate structured understanding.

4. The regular expression synthesis method based on large language model requirement understanding according to claim 1, characterized in that, In S2, the scorer uses the matching degree of candidate understandings on regular use cases as the evaluation index to score and rank each candidate understanding, and selects the best understanding in the current round. If the best understanding in the current round still fails to pass all regular use cases, the large language model is driven to iterate and generate a new round of candidate understandings. The scoring and selection process is repeated until the generated understandings meet all regular use cases or reach the preset maximum number of iterations, and finally the best understanding is output.

5. The regular expression synthesis method based on large language model requirement understanding according to claim 1, characterized in that, In S4, the reflection prompts include the candidate regular expressions that failed, the types of the failed test cases, and the regular expression test cases of the failed test cases. The reasons for failure are analyzed through the large language model to obtain the corresponding correction logic. Then, the scorer uses the consistency score between the corresponding correction logic and the failed test cases as the core evaluation index to score and rank each candidate reflection, and select the optimal reflection for the current round. If the optimal reflection for the current round still fails to pass all regular expression test cases, the large language model is driven to iterate and generate a new round of candidate reflections. The evaluation and selection process is repeated until all regular expression test cases are satisfied or the preset maximum number of iterations is reached, and finally the optimal reflection is output.

6. The regular expression synthesis method based on large language model requirement understanding according to claim 1, characterized in that, In S6, the syntax tree splitting specifically involves generating an abstract syntax tree through a regular expression parser and extracting each node into an independent sub-pattern; the model semantic splitting specifically involves calling a large language model to split the regular expression according to semantic boundaries, and verifying whether the recombined sub-patterns after splitting are equivalent to the original regular expression, thereby obtaining a list of sub-patterns.

7. The regular expression synthesis method based on large language model requirement understanding according to claim 1, characterized in that, In S6, by replacing each item in the subpattern list with a wildcard, it is determined whether the positive examples pass, thereby locating the erroneous subpattern. Specifically, this includes: splitting the candidate regular expression to obtain the subpattern list; replacing one or more subpatterns in the subpattern list with wildcards to form a temporary regular expression; if the temporary regular expression can pass all the regular positive examples, it is determined that there is a repairable erroneous subpattern or erroneous combination in the replaced area.

8. The regular expression synthesis method based on large language model requirement understanding according to claim 1, characterized in that, In S6, the recombination repair includes sequential rearrangement, enumerated deletion, and enumerated insertion; The specific process of the order rearrangement is as follows: perform full permutation and combination of the sub-patterns, and verify whether the new sequence passes all regular expression test cases; The enumeration deletion specifically involves: attempting to delete each sub-pattern and verifying whether the remaining combinations pass all regular expression test cases; if they do, the deleted sub-pattern is determined to be redundant. The enumeration insertion specifically involves: enumerating and inserting specified wildcards between sub-patterns, and calling single-point or multi-point repair modules to refine the wildcard region.

9. A regular expression synthesis device based on large language model requirement understanding, characterized in that, include: The refinement module is used to obtain the natural language description and regular expression test case set of user input; When either the natural language description or the set of regular expression test cases is missing, the large language model is called to complete the missing information. Then, the large language model is called again to refine the natural language description and the set of regular expression test cases, and the refined natural language description and the set of regular expression test cases are output. The optimal understanding filtering module is used to construct requirement understanding prompt words based on refined natural language descriptions and regular use case sets, and input them into a large language model to generate multiple candidate understandings. The scoring unit scores the matching performance of each candidate understanding on regular use cases and filters out the optimal understanding. The validation module is used to input the optimally understood and refined natural language description and regular expression test case set into the large language model, generate candidate regular expressions, perform positive and negative example validation on the candidate regular expressions, and output the candidate regular expressions and the regular expression test cases that fail. The optimal reflection filtering module is used to construct reflection prompt words based on the failed candidate regular expressions and failed regular expression use cases, and input them into the large language model to generate multiple candidate reflections; the scorer scores the consistency performance of each candidate reflection on the failed regular expression use cases, and filters out the optimal reflection. The repair module is used to input the best reflection, the failed candidate regular expressions and the failed regular expression test cases into the large language model for repair, generate the repaired regular expressions, perform positive and negative example verification, and output the regular expression test cases that still fail. The synthesis module is used to determine if there are still any regular expression test cases that fail. If so, the local repair module is activated to split the current regular expression into a syntax tree or model semantics to obtain a sub-pattern list. By replacing each item in the sub-pattern list with a wildcard, it is determined whether the positive example passes, thereby locating the erroneous sub-pattern. Then, it uses reorganization repair, single-point repair, adjacent window repair, non-adjacent merge block repair, or multi-point independent repair strategies to call the sub-pattern refinement module to accurately repair the local sub-pattern, generate the repaired regular expression, and perform overall verification until all regular expression test cases or the maximum number of repairs is reached, confirming that the regular expression synthesis is complete.

Citation Information

Patent Citations

  • Auxiliary teaching interaction method and device for regular language and automaton theory

    CN118627940A

  • Medical document regular expression generation method and device based on large language model

    CN118709661A