首页 > 其他分享 >Generates Regular Expressions That Match A Set of Strings

Generates Regular Expressions That Match A Set of Strings

时间:2022-12-20 20:33:32浏览次数:79  
标签:Set Generates trie expression regexgen Regular DFA

GitHub - devongovett/regexgen: Generate regular expressions that match a set of strings

How does it work?

  1. Generate a Trie containing all of the input strings. This is a tree structure where each edge represents a single character.
  2. A trie can be seen as a tree-shaped deterministic finite automaton (DFA), so DFA algorithms can be applied. In this case, we apply Hopcroft's DFA minimization algorithm to merge the nondistinguishable states.
  3. Convert the resulting minimized DFA to a regular expression. This is done using Brzozowski's algebraic method.

改动:不用装Node.js,打开test.html即可:

<script src='jsesc.js'></script>
<script src='regenerate.js'></script>
<!-- This class extends the native ES6 Set class with some additional methods -->
<script src='set.js'></script>
<!-- This ES6 Map subclass calls the getter function passed to the constructor to initialize
     undefined properties when they are first retrieved. -->
<script src='map.js'></script>
<!-- Represents a state in a DFA. -->
<script src='state.js'></script>
<!-- Implements Hopcroft's DFA minimization algorithm. -->
<script src='minimize.js'></script>
<!-- Represents an alternation (e.g. `foo|bar`) -->
<script src='ast.js'></script>
<!-- Implements Brzozowski's algebraic method to convert a DFA into a regular expression pattern. -->
<script src='regex.js'></script>
<!-- A Trie represents a set of strings in a tree data structure where each edge represents a single character. -->
<script src='trie.js'></script>
<script>
/**
 * Generates a regular expression that matches the given input strings.
 * @param {Array<string>} inputs
 * @param {string} flags
 * @return {RegExp}
 */
function regexgen(inputs, flags) {
  let trie = new Trie;
  trie.addAll(inputs);
  return trie.toRegExp(flags);
}
function print(s) { document.write(s + '<br>'); console.log(s + '\n'); }
let args = [ 'a', 'aa', 'aaa'];
print(regexgen(args, ''));
</script>

https://files.cnblogs.com/files/blogs/714801/regexgen-devongovett.7z 14KB 含test.hml及所有.js

  • 下载后不能运行,把Map改成DefaultMap,Set换成ExtendedSet后好了。
  • => 是箭头函数
  • (?: subexpression ) Defines a noncapturing group. When you want to have parenthesis but not capture the sub-expression you use non-capturing groups.
  • 例子 | 文档1 | 文档2 | 动手试一试

标签:Set,Generates,trie,expression,regexgen,Regular,DFA
From: https://www.cnblogs.com/funwithwords/p/16995043.html

相关文章