如何从给定的字符串列表自动生成正则表达式?

前端之家收集整理的这篇文章主要介绍了如何从给定的字符串列表自动生成正则表达式?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
您将获得2个Strings – A和B的列表。找到匹配A中所有字符串的最短正则表达式,否则为B.请注意,该正则表达式可以匹配/不匹配不在A中而不是B中的其他字符串。简单,我们可以假设我们的字母大小只有2个字符 – 0和1.也只允许这些运算符:

* – 0或以上
? – 0或1
– 1个或更多
() – 括号

为了简单起见,不允许使用正则表达式运算符。我不知道是否允许或者操作符(|)来简化问题。 A和B之间没有共同点。这里有些例子:

  1. A=[00,01,10]
  2. B=[11]
  3. answer = 1*0+1*
  1. A=[00,11]
  2. B=[10]
  3. answer = 0*1*
一种解决这个问题的方法是使用遗传算法。我碰巧有一个 genetic solver laying around,所以我使用以下算法将其应用于您的问题:

从所需投入中获取不同的标记作为基因
>为基因添加正则表达式特效
>适合于算法

>确保生成的字符串是一个有效的正则表达式
>根据匹配多少个所需的事物获得适合度值
有多少不合需要的东西它匹配

>直到找到一个成功的正则表达式

>从不同的令牌的数量开始,并根据需要递增
>尝试生成一个传递健身要求的长度的正则表达式

这是我在C#中的实现

  1. private static void GenerateRegex(IEnumerable<string> target,IEnumerable<string> dontMatch)
  2. {
  3. string distinctSymbols = new String(target.SelectMany(x => x).Distinct().ToArray());
  4. string genes = distinctSymbols + "?*()+";
  5.  
  6. Func<string,uint> calcFitness = str =>
  7. {
  8. if (str.Count(x => x == '(') != str.Count(x => x == ')'))
  9. {
  10. return Int32.MaxValue;
  11. }
  12. if ("?*+".Any(x => str[0] == x))
  13. {
  14. return Int32.MaxValue;
  15. }
  16. if ("?*+?*+".ToArray().Permute(2)
  17. .Any(permutation => str.IndexOf(new string(permutation.ToArray())) != -1))
  18. {
  19. return Int32.MaxValue;
  20. }
  21. Regex regex;
  22. try
  23. {
  24. regex = new Regex("^" + str + "$");
  25. }
  26. catch (Exception)
  27. {
  28. return Int32.MaxValue;
  29. }
  30. uint fitness = target.Aggregate<string,uint>(0,(current,t) => current + (regex.IsMatch(t) ? 0U : 1));
  31. uint nonFitness = dontMatch.Aggregate<string,t) => current + (regex.IsMatch(t) ? 10U : 0));
  32. return fitness + nonFitness;
  33. };
  34.  
  35. for (int targetGeneLength = distinctSymbols.Length; targetGeneLength < genes.Length * 2; targetGeneLength++)
  36. {
  37. string best = new GeneticSolver(50).GetBestGenetically(targetGeneLength,genes,calcFitness,true);
  38. if (calcFitness(best) != 0)
  39. {
  40. Console.WriteLine("-- not solved with regex of length " + targetGeneLength);
  41. continue;
  42. }
  43. Console.WriteLine("solved with: " + best);
  44. break;
  45. }
  46. }

并将其应用于您的样品的结果:

  1. public void Given_Sample_A()
  2. {
  3. var target = new[] { "00","01","10" };
  4. var dontMatch = new[] { "11" };
  5.  
  6. GenerateRegex(target,dontMatch);
  7. }

输出

  1. Generation 1 best: 10 (2)
  2. Generation 2 best: 0+ (2)
  3. Generation 5 best: 0* (2)
  4. Generation 8 best: 00 (2)
  5. Generation 9 best: 01 (2)
  6. -- not solved with regex of length 2
  7. Generation 1 best: 10* (2)
  8. Generation 3 best: 00* (2)
  9. Generation 4 best: 01+ (2)
  10. Generation 6 best: 10+ (2)
  11. Generation 9 best: 00? (2)
  12. Generation 11 best: 00+ (2)
  13. Generation 14 best: 0?1 (2)
  14. Generation 21 best: 0*0 (2)
  15. Generation 37 best: 1?0 (2)
  16. Generation 43 best: 10? (2)
  17. Generation 68 best: 01* (2)
  18. Generation 78 best: 1*0 (2)
  19. Generation 79 best: 0*1 (2)
  20. Generation 84 best: 0?0 (2)
  21. Generation 127 best: 01? (2)
  22. Generation 142 best: 0+1 (2)
  23. Generation 146 best: 0+0 (2)
  24. Generation 171 best: 1+0 (2)
  25. -- not solved with regex of length 3
  26. Generation 1 best: 1*0+ (1)
  27. Generation 2 best: 0+1* (1)
  28. Generation 20 best: 1?0+ (1)
  29. Generation 31 best: 1?0* (1)
  30. -- not solved with regex of length 4
  31. Generation 1 best: 1*00? (1)
  32. Generation 2 best: 0*1?0 (1)
  33. Generation 3 best: 1?0?0 (1)
  34. Generation 4 best: 1?00? (1)
  35. Generation 8 best: 1?00* (1)
  36. Generation 12 best: 1*0?0 (1)
  37. Generation 13 best: 1*00* (1)
  38. Generation 41 best: 0*10* (1)
  39. Generation 44 best: 1*0*0 (1)
  40. -- not solved with regex of length 5
  41. Generation 1 best: 0+(1)? (1)
  42. Generation 36 best: 0+()1? (1)
  43. Generation 39 best: 0+(1?) (1)
  44. Generation 61 best: 1*0+1? (0)
  45. solved with: 1*0+1?

第二个样本:

  1. public void Given_Sample_B()
  2. {
  3. var target = new[] { "00","11" };
  4. var dontMatch = new[] { "10" };
  5.  
  6. GenerateRegex(target,dontMatch);
  7. }

输出

  1. Generation 1 best: 00 (2)
  2. Generation 2 best: 01 (2)
  3. Generation 7 best: 0* (2)
  4. Generation 12 best: 0+ (2)
  5. Generation 33 best: 1+ (2)
  6. Generation 36 best: 1* (2)
  7. Generation 53 best: 11 (2)
  8. -- not solved with regex of length 2
  9. Generation 1 best: 00* (2)
  10. Generation 2 best: 0+0 (2)
  11. Generation 7 best: 0+1 (2)
  12. Generation 12 best: 00? (2)
  13. Generation 15 best: 01* (2)
  14. Generation 16 best: 0*0 (2)
  15. Generation 19 best: 01+ (2)
  16. Generation 30 best: 0?0 (2)
  17. Generation 32 best: 0*1 (2)
  18. Generation 42 best: 11* (2)
  19. Generation 43 best: 1+1 (2)
  20. Generation 44 best: 00+ (2)
  21. Generation 87 best: 01? (2)
  22. Generation 96 best: 0?1 (2)
  23. Generation 125 best: 11? (2)
  24. Generation 126 best: 1?1 (2)
  25. Generation 135 best: 11+ (2)
  26. Generation 149 best: 1*1 (2)
  27. -- not solved with regex of length 3
  28. Generation 1 best: 0*1* (0)
  29. solved with: 0*1*

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