| 1 |
Reg-gen |
| 2 |
======= |
| 3 |
|
| 4 |
This package generates strings based on regular expressions |
| 5 |
|
| 6 |
# Try it [here](https://lucasjones.github.io/reggen) |
| 7 |
|
| 8 |
Usage |
| 9 |
===== |
| 10 |
|
| 11 |
```go |
| 12 |
package main |
| 13 |
|
| 14 |
import ( |
| 15 |
"fmt" |
| 16 |
|
| 17 |
"github.com/lucasjones/reggen" |
| 18 |
) |
| 19 |
|
| 20 |
func main() { |
| 21 |
// generate a single string |
| 22 |
str, err := reggen.Generate("^[a-z]{5,10}@[a-z]{5,10}\\.(com|net|org)$", 10) |
| 23 |
if err != nil { |
| 24 |
panic(err) |
| 25 |
} |
| 26 |
fmt.Println(str) |
| 27 |
|
| 28 |
// create a reusable generator |
| 29 |
g, err := reggen.NewGenerator("[01]{5}") |
| 30 |
if err != nil { |
| 31 |
panic(err) |
| 32 |
} |
| 33 |
|
| 34 |
for i := 0; i < 5; i++ { |
| 35 |
// 10 is the maximum number of times star, range or plus should repeat |
| 36 |
// i.e. [0-9]+ will generate at most 10 characters if this is set to 10 |
| 37 |
fmt.Println(g.Generate(10)) |
| 38 |
} |
| 39 |
} |
| 40 |
``` |
| 41 |
|
| 42 |
### Sample output: |
| 43 |
|
| 44 |
``` |
| 45 |
[email protected] |
| 46 |
11000 |
| 47 |
01010 |
| 48 |
01100 |
| 49 |
01111 |
| 50 |
01001 |
| 51 |
``` |