
Randexp.js: Create random strings that match a given regular expression - benwithem
https://github.com/fent/randexp.js
======
misterdata
Warp [1] implements this, and also allows you to generate a list of _all_
possible values matching a particular regex (the number of possibilities of
course grows quite quickly for many regexes!).

[1]
[https://itunes.apple.com/us/app/warp/id973942134?mt=12](https://itunes.apple.com/us/app/warp/id973942134?mt=12)

~~~
dnet
Exrex does the same in Python and much more:

    
    
       - Generating all matching strings
       - Generating a random matching string
       - Counting the number of matching strings
       - Simplification of regular expressions
    

[https://github.com/asciimoo/exrex](https://github.com/asciimoo/exrex) (AGPL
licensed)

~~~
omegote
That's cool, thanks. I've discovered sre_parse thanks to that link, so cool
too.

------
pimpl
Do you think it’s safe to generate ticket numbers with such tools? I'm
currently this PHP lib to do it:
[https://github.com/icomefromthenet/ReverseRegex](https://github.com/icomefromthenet/ReverseRegex)
and since we do not sell thousands of tickets a day I think it should be okay
to use it for this purpose. What do you think?

~~~
frobaldo
It's practical, if you check the new one is not a duplicate - the lib info
does not state how distinct the values would be, or how does it uses the RNG.

Depending on what you really need (output string format, amount of numbers,
non-additive links), you should find a hash function for your fit. You can
always start by looking for youtube url hash.

------
rolfvandekrol
My new favourite password generator is now
[http://fent.github.io/randexp.js/#r=%5B%5E%5Cs%5D%7B16%7D&i](http://fent.github.io/randexp.js/#r=%5B%5E%5Cs%5D%7B16%7D&i)

~~~
bmm6o
I'd be surprised if it had the amount of entropy you need. I haven't looked at
the source, but javascript in a browser is not a good source of random
numbers.

------
tyho
The famous email regex ([http://www.ex-parrot.com/pdw/Mail-
RFC822-Address.html](http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html)):

[https://tinyurl.com/qz2ykex](https://tinyurl.com/qz2ykex)

~~~
benwithem
Safari loved that.

------
henningpeters
Would be great to integrate it into a live regex editor such as
[http://www.regexr.com/](http://www.regexr.com/)

------
TheLoneWolfling
So what happens when you pass something like `(?!.?|(..+?)\\\1+)

Or does it not support lookahead?

------
__john
What's the practical use for this...? Input validation?

~~~
luiz-pv9
According to the readme:

"Regular expressions are used in every language, every programmer is familiar
with them. Regex can be used to easily express complex strings. What better
way to generate a random string than with a tool you can easily use to express
the string any way you want?"

An example where this is useful (I guess) is generating data for tests. You
can easily define an email regexp to generate random "valid" values for each
model.

~~~
zeroonetwothree
For testing you'd want to generate strings non-uniformly, i.e. you want to hit
edge cases rather than test 1 billion similar 20 character length e-mails.

~~~
luiz-pv9
That's true. What I meant was that this tool is good to support testing the
same way factories work: generating random valid emails, phones, addresses,
etc., for each record.

------
soft_dev_person
If anyone misses a demo:
[http://jsfiddle.net/n3dgjLo5/](http://jsfiddle.net/n3dgjLo5/)

------
pavel_lishin
This is _great_ for regex crossword generators!

