Sunday 21 January 2018

java - Using Regex to generate Strings rather than match them

itemprop="text">

I am writing a Java utility which
helps me to generate loads of data for performance testing. It would be
really cool to be able to specify a regex for Strings so that my
generator spits out things which match this. Is there something out there already baked
which I can use to do this? Or is there a library which gets me most of the way
there?



Thanks



Answer





Edit:



As mentioned in the comments, there is a
library available at Google Code to acheive this:
href="http://code.google.com/p/xeger"
rel="noreferrer">http://code.google.com/p/xeger



See
also rel="noreferrer">https://github.com/mifmif/Generex as suggested by href="https://stackoverflow.com/a/24659605/1820">Mifmif



Original
message:



Firstly, with a complex
enough regexp, i believe this can be impossible. But you should be able to put something
together for simple regexps.




If you
take a look at the source code of the class java.util.regex.Pattern, you'll see that it
uses an internal representation of Node instances. Each of the different pattern
components have their own implementation of a Node subclass. These Nodes are organised
into a tree.



By producing a visitor that
traverses this tree, you should be able to call an overloaded generator method or some
kind of Builder that cobbles something together.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...