具有正则表达式的字符串生成,如标准

前端之家收集整理的这篇文章主要介绍了具有正则表达式的字符串生成,如标准前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不知道实现一个满足以下第二个想法要求的最佳字符串生成器类是可行的:

>使用regex生成标准
> Lexicographical order枚举.
计数推理
>索引访问

我不喜欢正则表达式:我不能想出一个开始的代码,但我只是想到一个天真的实现使用TList作为基类,并使用一个过滤器(Regex)与“强力”生成的字符串.

什么是其他最佳选择?

>订购:首先按长度(最短),然后按字典顺序排列.
>生成中使用的字符范围的规范:[A-Z],[a-z],数字,特殊符号和最终空格(正则表达式)的所有可打印或任何可能的组合.
>带有给定最小/最大值的字符串长度.
>搜索空间用边界约束:开始字符串一个可能性过滤的结束字符串(regex?)

最后编辑

首先,我使用正则表达式代替正则表达式来改写标题.

我正在考虑修改第一个要求,因为它是一个敞开的门,可能导致不合理的问题.

我需要建议和帮助正确的措辞.

第二个想法要求编辑完成.仍然愿意提出改进建议.

我会通过构建语言的最低 Deterministic Finite Automaton 来做到这一点.如果您正在使用正则表达式,则可以通过Thompson的结构自动完成,然后再进行子集构造和最小化.例如见 this description.

使用DFA,您可以使用以下算法:

  1. Let P = { < START,[""] > } be a set of pairs <State,list of strings>
  2. for n = 0,1,... Max
  3. Let P' = {} be a new set
  4. while P is not empty
  5. Remove the pair <s,L> from P
  6. For each transition s -- c --> t in alpha order of c
  7. if t is an accepting state,output l + c for each string l in L
  8. put <t,L + c> in P' (** i.e. append c to each string in L)
  9. end
  10. Set P = P'
  11. end

请注意,标记为**的步骤需要设置为true,因为重复项可以轻松地出现.

这是一个核心算法. P可以随着输出长度呈指数增长,但这只是跟踪未来输出字符串的所有可能性的代价.您可以通过在列表L中维护排序顺序并通过在达到资源限制时切断搜索来确保您提到的订单/大小/空间限制.

编辑

这里是一个玩具Java示例,其中我已经将DFA用简单的二进制浮点文字编码,并带有可选的减号.这使用与上面的伪代码稍微不同的方案来获得输出的严格排序顺序和适应字符范围.

  1. import java.util.Comparator;
  2. import java.util.TreeSet;
  3.  
  4. public class Test{
  5.  
  6. public static class DFA {
  7.  
  8. public static class Transition {
  9.  
  10. final int to;
  11. final char lo,hi; // Character range.
  12.  
  13. public Transition(int to,char lo,char hi) {
  14. this.to = to;
  15. this.lo = lo;
  16. this.hi = hi;
  17. }
  18.  
  19. public Transition(int to,char ch) {
  20. this(to,ch,ch);
  21. }
  22. }
  23.  
  24. // transitions[i] is a vector of transitions from state i.
  25. final Transition [] [] transitions;
  26.  
  27. // accepting[i] is true iff state i is accepting
  28. final boolean [] accepting;
  29.  
  30. // Make a fresh immutable DFA.
  31. public DFA(Transition [] [] transitions,boolean [] accepting) {
  32. this.transitions = transitions;
  33. this.accepting = accepting;
  34. }
  35.  
  36. // A pair is a DFA state number and the input string read to get there.
  37. private static class Pair {
  38. final int at;
  39. final String s;
  40.  
  41. Pair(int at,String s) {
  42. this.at = at;
  43. this.s = s;
  44. }
  45. }
  46.  
  47. // Compare pairs ignoring `at` states,since
  48. // they are equal iff the strings are equal.
  49. private Comparator<Pair> emitOrder = new Comparator<Pair>() {
  50. @Override
  51. public int compare(Pair a,Pair b) {
  52. return a.s.compareTo(b.s);
  53. }
  54. };
  55.  
  56. // Emit all strings accepted by the DFA of given max length.
  57. // Output is in sorted order.
  58. void emit(int maxLength) {
  59. TreeSet<Pair> pairs = new TreeSet<Pair>(emitOrder);
  60. pairs.add(new Pair(0,""));
  61. for (int len = 0; len <= maxLength; ++len) {
  62. TreeSet<Pair> newPairs = new TreeSet<Pair>(emitOrder);
  63. while (!pairs.isEmpty()) {
  64. Pair pair = pairs.pollFirst();
  65. for (Transition x : transitions[pair.at]) {
  66. for (char ch = x.lo; ch <= x.hi; ch++) {
  67. String s = pair.s + ch;
  68. if (newPairs.add(new Pair(x.to,s)) && accepting[x.to]) {
  69. System.out.println(s);
  70. }
  71. }
  72. }
  73. }
  74. pairs = newPairs;
  75. }
  76. }
  77. }
  78.  
  79. // Emit with a little DFA for floating point numbers.
  80. public void run() {
  81. DFA.Transition [] [] transitions = {
  82. { // From 0
  83. new DFA.Transition(1,'-'),new DFA.Transition(2,'.'),new DFA.Transition(3,'0','1'),},{ // From 1
  84. new DFA.Transition(2,{ // From 2
  85. new DFA.Transition(4,{ // From 3
  86. new DFA.Transition(3,new DFA.Transition(4,{ // From 4
  87. new DFA.Transition(4,}
  88. };
  89. boolean [] accepting = { false,false,true,true };
  90. new DFA(transitions,accepting).emit(4);
  91. }
  92.  
  93. public static void main (String [] args) {
  94. new Test().run();
  95. }
  96. }

猜你在找的正则表达式相关文章