Skip to content

Commit 446ad33

Browse files
yabanciyabanciclaudekashifkhan0771
authored
feat(regexamples): add regex example generation package (#239)
* feat(regexamples): add regex example generation package Adds a new `regexamples` package that generates random strings matching a given regular expression. Supports all common regex constructs via Go's stdlib `regexp/syntax` AST walker, restricts output to printable ASCII, and exposes both a one-shot `Generate` function and a reusable `Generator` type with optional deterministic seeding via `SetSeed`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(regexamples): clean up dead code, add concurrency docs, and list in main README - Remove unreachable defensive checks in pickFromRanges and randPrintable - Eliminate redundant countRunes call via pickFromRangesN helper - Add concurrency safety doc comment on Generator - Fix relative link in regexamples/README.md - Add regexamples package to main README table Reviewed and applied via Claude Code Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: yabanci <yabanciinbt@gmail.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Kashif Khan <kashifkhan_9@yahoo.com>
1 parent 4e3ae06 commit 446ad33

File tree

5 files changed

+792
-0
lines changed

5 files changed

+792
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ cd utils
6363
| **queue** | Queue data structure| [README](queue/README.md) | [EXAMPLES](queue/EXAMPLES.md) |
6464
| **rand** | Random number and string generation utilities | [README](rand/README.md) | [EXAMPLES](rand/EXAMPLES.md) |
6565
| **ratelimiter** | Token-bucket rate limiter (allow/wait, adjustable capacity & refill rate) | [README](ratelimiter/README.md) | [EXAMPLES](ratelimiter/EXAMPLES.md) |
66+
| **regexamples** | Generate random strings that match a given regular expression | [README](regexamples/README.md) | [EXAMPLES](regexamples/EXAMPLES.md) |
6667
| **slice** | Slice manipulation and de-duplication utilities | [README](slice/README.md) | [EXAMPLES](slice/EXAMPLES.md) |
6768
| **slugger** | A simple and efficient way to generate URL-friendly slugs from strings | [README](slugger/README.md) | [EXAMPLES](slugger/EXAMPLES.md) |
6869
| **sort** | Sorting algorithms | [README](sort/README.md) | [EXAMPLES](sort/EXAMPLES.md) |

regexamples/EXAMPLES.md

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
## Regex Examples Function Examples
2+
3+
### Generate examples for a digit pattern
4+
5+
```go
6+
package main
7+
8+
import (
9+
"fmt"
10+
11+
"github.com/kashifkhan0771/utils/regexamples"
12+
)
13+
14+
func main() {
15+
examples, err := regexamples.Generate(`\d{4}`, 5)
16+
if err != nil {
17+
fmt.Println("Error:", err)
18+
return
19+
}
20+
21+
for _, s := range examples {
22+
fmt.Println(s)
23+
}
24+
}
25+
```
26+
27+
#### Output:
28+
29+
```
30+
7392
31+
0154
32+
8820
33+
3467
34+
5901
35+
```
36+
37+
---
38+
39+
### Generate examples for a word pattern with a range quantifier
40+
41+
```go
42+
package main
43+
44+
import (
45+
"fmt"
46+
47+
"github.com/kashifkhan0771/utils/regexamples"
48+
)
49+
50+
func main() {
51+
examples, err := regexamples.Generate(`\w{3,6}`, 5)
52+
if err != nil {
53+
fmt.Println("Error:", err)
54+
return
55+
}
56+
57+
for _, s := range examples {
58+
fmt.Println(s)
59+
}
60+
}
61+
```
62+
63+
#### Output:
64+
65+
```
66+
aB3
67+
kQz19w
68+
mR7
69+
x2Lp
70+
Tz4kWn
71+
```
72+
73+
---
74+
75+
### Generate examples for an alternation pattern
76+
77+
```go
78+
package main
79+
80+
import (
81+
"fmt"
82+
83+
"github.com/kashifkhan0771/utils/regexamples"
84+
)
85+
86+
func main() {
87+
examples, err := regexamples.Generate(`(foo|bar|baz)`, 5)
88+
if err != nil {
89+
fmt.Println("Error:", err)
90+
return
91+
}
92+
93+
for _, s := range examples {
94+
fmt.Println(s)
95+
}
96+
}
97+
```
98+
99+
#### Output:
100+
101+
```
102+
bar
103+
foo
104+
baz
105+
foo
106+
bar
107+
```
108+
109+
---
110+
111+
### Generate examples for an email-like pattern
112+
113+
```go
114+
package main
115+
116+
import (
117+
"fmt"
118+
119+
"github.com/kashifkhan0771/utils/regexamples"
120+
)
121+
122+
func main() {
123+
examples, err := regexamples.Generate(`[a-z]{4,8}@[a-z]{3,6}\.(com|net|org)`, 5)
124+
if err != nil {
125+
fmt.Println("Error:", err)
126+
return
127+
}
128+
129+
for _, s := range examples {
130+
fmt.Println(s)
131+
}
132+
}
133+
```
134+
135+
#### Output:
136+
137+
```
138+
jktr@mxpw.com
139+
abcdefgh@zlo.net
140+
pwqr@abcde.org
141+
mnop@xyz.com
142+
lkjh@qrst.net
143+
```
144+
145+
---
146+
147+
### Reuse a Generator for multiple batches
148+
149+
```go
150+
package main
151+
152+
import (
153+
"fmt"
154+
155+
"github.com/kashifkhan0771/utils/regexamples"
156+
)
157+
158+
func main() {
159+
g, err := regexamples.NewGenerator(`[A-Z]{2}\d{4}`)
160+
if err != nil {
161+
fmt.Println("Error:", err)
162+
return
163+
}
164+
165+
for range 3 {
166+
batch, err := g.Generate(3)
167+
if err != nil {
168+
fmt.Println("Error:", err)
169+
return
170+
}
171+
172+
fmt.Println(batch)
173+
}
174+
}
175+
```
176+
177+
#### Output:
178+
179+
```
180+
[KT8301 PX4720 BN0093]
181+
[ZA1154 MQ6688 RL2279]
182+
[CF9900 YD3341 WS7712]
183+
```
184+
185+
---
186+
187+
### Deterministic generation with SetSeed
188+
189+
```go
190+
package main
191+
192+
import (
193+
"fmt"
194+
195+
"github.com/kashifkhan0771/utils/regexamples"
196+
)
197+
198+
func main() {
199+
g, err := regexamples.NewGenerator(`[a-z]{5}-\d{3}`)
200+
if err != nil {
201+
fmt.Println("Error:", err)
202+
return
203+
}
204+
205+
g.SetSeed(42)
206+
207+
examples, err := g.Generate(3)
208+
if err != nil {
209+
fmt.Println("Error:", err)
210+
return
211+
}
212+
213+
for _, s := range examples {
214+
fmt.Println(s)
215+
}
216+
}
217+
```
218+
219+
#### Output:
220+
221+
```
222+
mxkqr-581
223+
azpwt-047
224+
lbnvc-923
225+
```
226+
227+
---

regexamples/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### Regex Examples (regexamples)
2+
3+
- **Generate**: Generates n random strings that match the given regular expression pattern.
4+
- **NewGenerator**: Compiles a regex pattern into a reusable Generator, avoiding repeated parsing overhead when generating multiple batches.
5+
- **Generator.Generate**: Generates n matching strings from a pre-compiled Generator.
6+
- **Generator.SetSeed**: Sets a deterministic seed on the Generator so that subsequent calls produce the same sequence of strings.
7+
8+
## Examples:
9+
10+
For examples of each function, please checkout [EXAMPLES.md](./EXAMPLES.md)
11+
12+
---

0 commit comments

Comments
 (0)