枚举Java中正则表达式的可能匹配项 [英] Enumerate Possible Matches of Regular Expression in Java

查看:373
本文介绍了枚举Java中正则表达式的可能匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,我想枚举Java中有限正则表达式的所有可能值。



对于某些情况,我有一个正则表达式用于匹配文字中允许的颜色值。下面是它的简化版本:

 (white | black)|((light | dark))?(red | | green | blue | gray)

我想创建一个可以枚举所有这些值的单元测试,将它们中的每一个传递给我的实用程序类,这些实用程序类会从中生成 Color 对象,这样,如果我更改正则表达式,则在发生错误时我的单元测试将失败(即不支持新的颜色值。)



我当然知道枚举是可能的(),但是是否存在Java的现有库,该库将枚举正则表达式的所有可能匹配项?



编辑::我已经实现了一个库。

解决方案

您是对的,没有在网上找到这样的工具
但是您可以尝试
Xeger 来自Google



<它可以从一个正则表达式中创建一个随机匹配的字符串,并且通过一些代码调整可能会达到您想要的效果。
生成一个随机匹配项:

 字符串regex = [ab] {4,6} c; 
Xeger generator = new Xeger(regex);
字符串结果= generator.generate();
assert result.matches(regex);

Xeger代码非常简单,它由2个文件组成,它们之间包含5种方法。 />
使用dk.brics.automaton将正则表达式转换为自动机,然后遍历自动机转换,在每个节点中进行随机选择。



the主要功能是生成:

  private void generate(StringBuilder builder,State state){
List< Transition> transitions = state.getSortedTransitions(true);
if(transitions.size()== 0){
assert state.isAccept();
的回报;
}
int nroptions = state.isAccept()吗? transitions.size():transitions.size()-1;
int选项= XegerUtils.getRandomInt(0,nroptions,随机);
if(state.isAccept()&& option == 0){// 0被视为止损
return;
}
//继续进行下一个转换
转换transition = transitions.get(option-(state.isAccept()?1:0));
appendChoice(builder,transition);
generate(builder,transition.getDest());
}

您可以看到要进行更改,以便获得所有可能的匹配项,您需要遍历每个可能节点中的所有可能组合(例如增加一个多位数字计数器)
,您将需要一个散列来防止循环,但是编码的时间不应超过5秒。



我也建议您先检查正则表达式是否确实是正则表达式,方法是检查它是否没有*,+和其他使该动作不可能的符号(只是为了使它成为完整的可重用工具)...


I want to enumerate all the possible values of a finite regular expression in Java for testing purposes.

For some context, I have a regular expression that I'm using to match allowable color values in words. Here's a shortened version of it as an example:

(white|black)|((light|dark) )?(red|green|blue|gray)

I wanted to create a unit test that would enumerate all these values and pass each of them to my utility class which produces a Color object from these, that way if I change the regular expression, my unit tests will fail if an error occurs (i.e. the new color value is unsupported).

I know enumeration is possible, of course (see this question), but is there an existing library for Java which will enumerate all the possible matches for a regex?

Edit: I've implemented a library that does this. See my answer below for links.

解决方案

You are right, didn't find such a tool online as well but you can try Xeger from google

it can create a random matching string from a regexp, and with some code tweaking might do what you want. generation a random match:

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

Xeger code is very simple, it consists of 2 files which contain 5 methods between them..
it uses dk.brics.automaton to conver the regex to an automaton, then goes over the automaton transitions making random choices in every node.

the main function is generate:

   private void generate(StringBuilder builder, State state) {
    List<Transition> transitions = state.getSortedTransitions(true);
    if (transitions.size() == 0) {
        assert state.isAccept();
        return;
    }
    int nroptions = state.isAccept() ? transitions.size() : transitions.size() - 1;
    int option = XegerUtils.getRandomInt(0, nroptions, random);
    if (state.isAccept() && option == 0) {          // 0 is considered stop
        return;
    }
    // Moving on to next transition
    Transition transition = transitions.get(option - (state.isAccept() ? 1 : 0));
    appendChoice(builder, transition);
    generate(builder, transition.getDest());
}

you can see that in order to change it so you get all possible matches, you need to iterate over all possible combinations in every possible node (like incrementing a multi digit counter) you will need a hash to prevent loops, but that shouldn't take more than 5 senconds to code..

i would also suggest first checking that the regex is actually finate, by checking that it doesn't have *,+ and other symbols that make this action impossible (just to make this a complete tool for reuse)...

这篇关于枚举Java中正则表达式的可能匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆