SplitRegex: Faster Regex Synthesis via Neural Example Splitting

Due to the practical importance of regular expressions ( regexes , for short ) , there has been a lot of research to automatically generate regexes from positive and negative string examples . A basic idea of learning a regex is a search-and-repair ; search for a correct regex and repair it if incorrect . The problem is known to be PSPACEcomplete and the main issue is to obtain a regex quickly within a time limit . While classical regex learning methods do not perform well , recent approaches using deep neural networks show better performance with respect to the accuracy of the resulting regexes . However , all these approaches including SOTA models are often extremely slow because of the slow searching mechanism , and do not produce desired regexes within a given time limit . We tackle the problem of learning regexes faster from positive and negative strings by relying on a novel approach called ‘ neural example splitting ’ . Our approach essentially split up example strings into multiple parts using a neural network trained to group similar substrings from positive strings . This helps to learn a regex faster and , thus , more accurately since we now learn from several short-length strings . We propose an effective regex synthesis framework called ‘ SplitRegex ’ that synthesizes subregexes from ‘ split ’ positive substrings and produces the final regex by concatenating the synthesized subregexes . For the negative sample , we exploit pre-generated subregexes during the subregex synthesis process and perform the matching against negative strings . Then the final regex becomes consistent with all negative strings . SplitRegex is a divided-and-conquer framework for learning target regexes ; split ( =divide ) positive strings and infer partial regexes for multiple parts , which is much more accurate than the whole string inferring , and concatenate ( =conquer ) inferred regexes while satisfying negative strings . We empirically demonstrate that the proposed SplitRegex framework substantially improves the previous regex synthesis approaches over four benchmark datasets . 1 INTRODUCTION . Regular expressions ( regexes in short ) are a powerful tool on processing sequence data in NLP or information retrieval . A regex—a finite sequence—is a formal representation of ( infinite ) symbol sequences . However , it is not easy to write a good regex for a set of sequences . Especially for those who have little experience on formal expressions , a single mistake in regex composition leads to an invalid regex or , even worse , a regex with different semantics to ones ’ intention . An automatic regex synthesis tries to moderate this human error . Examples ( Oncina & Garcı́a , 1992 ; Bartoli et al. , 2014 ) and natural language descriptions ( Locascio et al. , 2016 ; Zhong et al. , 2018b ) are the two major sources of regex synthesis . Examples are specific in their meaning without any implicit semantics , but are hard to provide the complete user intent described by a regex . On the other hand , natural language descriptions are ambiguous but describes the high-level concept of a regex . As these two different types of information are complementary to each other , there have been many studies ( Chen et al. , 2020 ; Li et al. , 2021 ; Zhong et al. , 2018a ) that suggest to use both modalities as they can use examples to fix the ambiguous expressions generated from natural language descriptions . The regex synthesis problem is to make a proper regex E from both positive and negative sets such that all the positive samples are generated by E and all the negative strings are not generated by E. This problem is also called the regex inference or learning problem in the literature . The traditional approaches for the regex synthesis problem are often inapplicable in practical settings . For example , Angluin ’ s ( 1987 ) L∗ is impractical because it requires the equivalence test between a target regex and a generated regex , which is known to be PSPACE-complete and thus intractable ( Stockmeyer & Meyer , 1973 ) . Gold ( 1978 ) shows that the state characterization approach is NP-complete and thus intractable . Heuristic approaches based on state-merging DFA induction algorithms such as RPNI ( Oncina & Garcı́a , 1992 ) and blue-fringe ( Lang et al. , 1998 ) are not very promising on synthesizing complex regexes because it is almost impossible to obtain a concise regex even when they are able to produce a concise DFA from examples . As recent approaches , both AlphaRegex ( Lee et al. , 2016 ) and RegexGenerator++ ( Bartoli et al. , 2014 ; 2016 ) generate candidate regexes and match against the given examples until the target regex matches all examples successfully . While these approaches generate correct regexes when a time limit is very long , they often fail when the example size is large . In addition , both approaches do not use any characteristics in the given examples , which can be a useful feature to speed up the process . In other words , these methods are impractical for generating regexes within a reasonable time limit . For practical usage , we notice that the divide-and-conquer approach is promising for synthesizing formal representation . For example , Alur et al . ( 2017 ) synthesize a program with hierarchical partition of the given input-output pairs . A decision tree reflects the hierarchical partition forms the control flow of the synthesized program . This method noticeably improves program synthesis time due to the partitioned problem space . Farzan & Nicolet ( 2021a ; b ) propose an effective method that converts a program into an equivalent program by splitting a problem into several subproblems . This divide-andconquer approach is efficient since individual tasks for solving subproblems can run simultaneously . Recently , Barke et al . ( 2020 ) ; Shrivastava et al . ( 2021 ) propose program synthesis methods that first finds partial solutions satisfying subsets of input-output examples and combines the solutions to find the global solution . In this paper , we propose a novel regex synthesis framework based on the divide-and-conquer paradigm . Given a set of positive examples , we split examples into multiple parts by grouping similar substrings using a neural network and solve subproblems defined over the multiple parts in serial order . We train a neural network called the NeuralSplitter that embeds a set of positive examples into a fixed-size vector and produces the most probable split for each example in the set . Our main contributions are as follows : 1 . We propose a regex synthesis framework SplitRegex based on the divide-and-conquer paradigm , which has been employed in many sequential algorithms . As far as we are aware , this is the first attempt to synthesize regexes in the divide-and-conquer manner . 2 . We design a neural network called the NeuralSplitter—a two-stage attention-based recurrent neural network trained to predict the most probable split of input positive strings . 3 . We conduct exhaustive experiments on various benchmark datasets including random dataset and practical regex datasets for demonstrating the effectiveness of the the proposed framework . We show that our framework can be easily combined with the off-the-shelf regex synthesis algorithms with minor modifications . Namely , our framework solves regex synthesis problem , which is PSPACE-complete , much faster when applied to any existing regex synthesis models , and thus boosts the overall performance . 2 RELATED WORK . We provide a list of related works on regex synthesis problems based on various input types including positive and negative examples and natural language descriptions . 2.1 REGULAR EXPRESSION INFERENCE FROM EXAMPLES . One approach to regular expression inference is by Angluin ’ s ( 1987 ) . Angluin suggests an algorithm L∗ that infers an unknown DFA in teacher-student architecture . The teacher of L∗ offers membership queries and equivalent queries on the target expression for the student . Note that this type of algorithms generally requires an infinite amount of examples to synthesize an FA . Gold ( 1978 ) proposes a heuristic inference algorithm that inductively distinguishes random prefixes according to the acceptance followed by the given examples . The prefixes with the same acceptance form a single state in a DFA . The blue-fringe algorithm ( Lang et al. , 1998 ) and RPNI ( Oncina & Garcı́a , 1992 ) are similar to the Gold ’ s algorithm . AlphaRegex ( Lee et al. , 2016 ) exhaustively searches through all possible regular expressions until it identifies a solution regex that satisfies given positive and negative examples . A major limit of AlphaRegex is that its runtime is extremely due to the exponential increase of search space . RegexGenerator++ ( Bartoli et al. , 2014 ; 2016 ) produces a regular expression using the genetic programming approach . The produced regular expression recognizes the surroundings ( context ) of the target sequence as well as the target itself . However , RegexGenerator++ entails a huge amount of computations due to the repetitive nature of genetic approach on each inference . 2.2 REGULAR EXPRESSION SYNTHESIS FROM NATURAL LANGUAGE DESCRIPTIONS . Ranta ( 1998 ) studies rule-based techniques for the conversion between multi-languages and regular expressions . Kushman & Barzilay ( 2013 ) build a parsing model that translates a natural language sentence to a regular expression , and provides a dataset , which is the most popular benchmark dataset in recent research . Locascio et al . ( 2016 ) propose the Deep-Regex model based on Seq2Seq for generating regular expressions from NL descriptions together with 10,000 NL-RX pair data . However , due to the limitations of the standard Seq2Seq model , the Deep-Regex model can only generate regular expressions similar in shape to the training data . The SemRegex model ( Zhong et al. , 2018b ) improves the Deep-Regex model by reinforcement learning based on the DFA-equivalence oracle , which determines if two regular expressions define the same language , as a reward function and their model can generate correct regular expressions even if they do not exactly look like answers . The SoftRegex model ( Park et al. , 2019 ) further improves SemRegex based on the fast regex equivalence test , which gives rise to a fast learning process . 2.3 MULTI-MODAL REGULAR EXPRESSION SYNTHESIS . Most multi-modal synthesis method uses natural language descriptions and positive/negative examples simultaneously to mitigate the ambiguity in natural languages . Chen et al . ( 2020 ) introduce hierarchical sketch for the semantics of natural description . Chen et al. ’ s implementation Regel first deduces the hierarchical structure representing the semantics of the natural language description . This semantic structure has some holes which the regular expression inference algorithm enhances a regular expression by predefined rules . Regel also prunes an infeasible expression using over- and under-approximation possible regular expressions , which replaces the holes with the most permissive and the most restrictive expression , respectively . Li et al . ( 2021 ) propose TransRegex framework that synthesizes a valid regular expression . TransRegex ’ s NLP-based synthesizer translates the given description to a regular expression and fixes the expression by rules and RFixer ( Pan et al. , 2019 ) to increase matching score on the examples . 2.4 REPAIR-BASE REGULAR EXPRESSION SYNTHESIS . Pan et al . ( 2019 ) design a heuristic algorithm RFixer that repairs a regular expression with given examples . RFixer improves the given regular expression by introducing or removing a hole and approximate the target expression with over- and under-approximation method until finds a regular expression with no holes that satisfies the given examples . Li et al . ( 2020 ) propose FlashRegex that generates a deterministic regular expression , which is a subclass of regular expressions . A SAT solver designs the basic FA structure after the given examples and FlashRegex iteratively shuffles the transitions in FA model at random to find the most suitable FA that satisfies the given examples .

Summary: This paper proposed a way of synthesizing RegEx from positive and negative sample specifications. The main idea is to split the positive samples into segments, such that one can leverage any existing RegEx synthesis tool to synthesize the sub-regex out of the segments of specifications. Finally the entire RegEx is obtained via the concatenation of these sub-RegExes. The paper proposes to split the specifications using RNN, which is trained via synthetically generated data. Experiments on synthetic and some benchmark datasets show that the proposed method is able to outperform the existing counterparts in most cases.