Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate strings by regexp in Scala

Tags:

regex

scala

Suppose there is a simple regular expression (only | and * are allowed). How would you create a stream of strings of any size (from smaller to larger) which match this regular expression in Scala?

like image 294
Michael Avatar asked Oct 23 '25 05:10

Michael


1 Answers

Use Xeger. Even though it's Java and not Scala, you should have no problem using it with Scala.

In Java:

String regex = "[ab]{4,6}c";
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);
like image 56
Wilfred Springer Avatar answered Oct 24 '25 20:10

Wilfred Springer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!