有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用正则表达式生成字符串而不是匹配字符串

我正在编写一个Java实用程序,它可以帮助我生成性能测试所需的数据负载。如果能够为字符串指定一个正则表达式,那么我的生成器将输出与此匹配的内容,这将是非常酷的。有没有什么东西已经烤好了,我可以用它来做这个?还是有一个图书馆能让我大部分时间都在那里

谢谢


共 (6) 个答案

  1. # 1 楼答案

    编辑:

    关于此问题的建议图书馆的完整列表:

    1. Xeger*-Java
    2. Generex*-Java
    3. Rgxgen-Java
    4. rxrdg-C#

    *-取决于dk.brics.automaton

    编辑: 如评论中所述,谷歌代码中提供了一个库来实现这一点: https://code.google.com/archive/p/xeger/

    另见Mifmif建议的https://github.com/mifmif/Generex

    原始消息:

    首先,对于足够复杂的regexp,我相信这是不可能的。但是您应该能够为简单的regexp组合一些东西

    如果你看一看java类的源代码。util。正则表达式。模式,您将看到它使用节点实例的内部表示。每个不同的模式组件都有自己的节点子类实现。这些节点被组织成一棵树

    通过生成一个遍历此树的访问者,您应该能够调用一个重载的生成器方法或某种将某些内容拼凑在一起的构建器

  2. # 2 楼答案

    现在帮助原创海报已经太晚了,但它可以帮助新来者Generex是一个有用的java库,它提供了许多使用正则表达式生成字符串的功能(随机生成、基于其索引生成字符串、生成所有字符串…)

    例如:

    Generex generex = new Generex("[0-3]([a-c]|[e-g]{1,2})");
    
    // generate the second String in lexicographical order that matches the given Regex.
    String secondString = generex.getMatchedString(2);
    System.out.println(secondString);// it print '0b'
    
    // Generate all String that matches the given Regex.
    List<String> matchedStrs = generex.getAllMatchedStrings();
    
    // Using Generex iterator
    Iterator iterator = generex.iterator();
    while (iterator.hasNext()) {
        System.out.print(iterator.next() + " ");
    }
    // it prints 0a 0b 0c 0e 0ee 0e 0e 0f 0fe 0f 0f 0g 0ge 0g 0g 1a 1b 1c 1e
    // 1ee 1e 1e 1f 1fe 1f 1f 1g 1ge 1g 1g 2a 2b 2c 2e 2ee 2e 2e 2f 2fe 2f 2f 2g
    // 2ge 2g 2g 3a 3b 3c 3e 3ee 3e 3e 3f 3fe 3f 3f 3g 3ge 3g 3g 1ee
    
    // Generate random String
    String randomStr = generex.random();
    System.out.println(randomStr);// a random value from the previous String list
    

    披露

    这篇文章中提到的项目属于回答这个问题的用户(MIF)。根据rules,这一点需要提出来

  3. # 3 楼答案

    这个问题真的很老了,尽管这个问题对我来说是实际存在的。 我试过xegerGenerex,但它们似乎不符合我的要求。 它们实际上无法处理某些正则表达式模式(如a{60000})或其他正则表达式模式(如(A|B|C|D|E|F)),它们只是不能生成所有可能的值。因为我没有找到任何其他合适的解决方案,所以我创建了自己的库

    https://github.com/curious-odd-man/RgxGen

    此库可用于生成匹配字符串和非匹配字符串

    maven central上也有可用的工件

    用法示例:

    RgxGen rgxGen = new RgxGen(aRegex);                     // Create generator
    String s = rgxGen.generate();                           // Generate new random value
    
  4. # 4 楼答案

    在stackoverflow播客11上:

    Spolsky: Yep. There's a new product also, if you don't want to use the Team System there our friends at Redgate have a product called SQL Data Generator [http://www.red-gate.com/products/sql_data_generator/index.htm]. It's $295, and it just generates some realistic test data. And it does things like actually generate real cities in the city column that actually exist, and then when it generates those it'll get the state right, instead of getting the state wrong, or putting states into German cities and stuff like... you know, it generates pretty realistic looking data. I'm not really sure what all the features are.

    这可能不是你想要的,但它可能是一个很好的起点,而不是创造你自己的

    我似乎在google中找不到任何东西,因此我建议通过将给定的正则表达式解析为最小的工作单元(\w、[x-x]、\d等)并编写一些基本方法来支持这些正则表达式短语来解决这个问题

    因此,对于\w,您将有一个方法getRandomLetter()返回任意随机字母,还有一个方法getRandomLetter(char startedter,char endLetter),它在两个值之间提供一个随机字母

  5. # 5 楼答案

    为此,我已经开始滚动我的own库(在c#中,但对于Java开发人员来说应该很容易理解)

    Rxrdg最初是为了解决为实际项目创建测试数据的问题。基本思想是利用现有的(正则表达式)验证模式来创建符合这些模式的随机数据。这样可以创建有效的随机数据

    为简单的正则表达式模式编写解析器并不困难。使用抽象语法树生成字符串应该更容易

  6. # 6 楼答案

    Xeger (Java)也有能力做到这一点:

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