繁体   English   中英

需要自动确定将导致正则表达式匹配的文本

[英]Need to automatically determine text that will cause a Regex match

我需要提供自动匹配正则表达式的文本。

例如,给定正则表达式: [Tt]rue我需要生成Truetrue

我只需要为每个Regex生成一个有效的匹配项,希望就Regex可能带来的无限可能性而言,这是一个更容易解决的问题。

由于正则表达式的构建方式,我不确定这是否可能。 另外,我不确定要搜索什么。 “倒置”比赛的更常见问题往往会淹没我的问题。

如果有关系,我正在使用C#。 如果解决方案需要另一种技术,那也很好。

回答:我被指向Xeger,这是一个Java库,导致我进入: https : //github.com/moodmosaic/Fare这是Xeger和dk.brics.automaton的C#端口,Xeger使用了C#端口。

您可以使用Xeger一个用于从正则表达式生成随机文本的Java库)来实现此目的

从其文档中说:

认为它与正则表达式匹配器相反。 该库允许您生成保证与传入的正则表达式匹配的文本

让我们以正则表达式为例:

[AB] {4,6-}℃

使用Xeger,您现在可以生成匹配此模式的字符串,如下所示:

String regex = "[ab]{4,6}c";
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);

Xeger网站还建议检查其局限性。 在这里,您可以找到它们通过限制定义的内容:

regexp  ::=     unionexp                
|                       
unionexp        ::=     interexp | unionexp     (union) 
|       interexp                
interexp        ::=     concatexp & interexp    (intersection)  [OPTIONAL]
|       concatexp               
concatexp       ::=     repeatexp concatexp     (concatenation) 
|       repeatexp               
repeatexp       ::=     repeatexp ?     (zero or one occurrence)        
|       repeatexp *     (zero or more occurrences)      
|       repeatexp +     (one or more occurrences)       
|       repeatexp {n}   (n occurrences) 
|       repeatexp {n,}  (n or more occurrences) 
|       repeatexp {n,m} (n to m occurrences, including both)    
|       complexp                
complexp        ::=     ~ complexp      (complement)    [OPTIONAL]
|       charclassexp            
charclassexp    ::=     [ charclasses ] (character class)       
|       [^ charclasses ]        (negated character class)       
|       simpleexp               
charclasses     ::=     charclass charclasses           
|       charclass               
charclass       ::=     charexp - charexp       (character range, including end-points) 
|       charexp         
simpleexp       ::=     charexp         
|       .       (any single character)  
|       #       (the empty language)    [OPTIONAL]
|       @       (any string)    [OPTIONAL]
|       " <Unicode string without double-quotes> "      (a string)      
|       ( )     (the empty string)      
|       ( unionexp )    (precedence override)   
|       < <identifier> >        (named automaton)       [OPTIONAL]
|       <n-m>   (numerical interval)    [OPTIONAL]
charexp ::=     <Unicode character>     (a single non-reserved character)       
|       \ <Unicode character>   (a single character)

我认为您应该测试简单的正则表达式并逐步添加更复杂的功能,以便确定它是否有助于您生成数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM