1

For example, given a pattern for matching email, I should get an array of strings that can be matched by the same regex pattern. Another example:

Input:

(a|b)[cd]{2}\1

Output:

acca 
adca 
acda 
adda 
bccb 
bdcb 
bcdb 
bddb 

I would like to use this library to generate dummy data for given pattern that can be used for testing my web app.

I found this website that has implemented it: https://regldg.com/tryit.php, but it is a binary executable. I'm looking for a library for PHP, JS, etc.

2 Answers 2

2

The python library rstr has the function xeger() to do what you need by using random strings and only returning ones that match:

Example

Install with pip install rstr

In [1]: from __future__ import print_function

In [2]: import rstr

In [3]: for dummy in range(10):
   ...:     print(rstr.xeger(r"(a|b)[cd]{2}\1"))
   ...:
acca
bddb
adda
bdcb
bccb
bcdb
adca
bccb
bccb
acda

Warning

For complex re patterns this might take a long time to generate any matches.

2

Does greenery do what you want? For instance,

>>> from greenery.lego import parse
>>> rep = parse('(a|b)[cd]{2}\1')
>>> list(rep.strings())
['acc\x01', 'acd\x01', 'adc\x01', 'add\x01', 'bcc\x01', 'bcd\x01', 'bdc\x01', 'bdd\x01']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.