1
1
# rexexp-gen
2
2
3
- [ ![ Maven Central ] ( https://maven-badges.herokuapp.com/ maven-central/org.cornutum.regexp/regexp-gen/badge .svg?style=plastic )] ( https://maven-badges.herokuapp.com/ maven-central/org.cornutum.regexp/ regexp-gen )
4
- [ ![ javadoc ] ( https://javadoc. io/badge2/org.cornutum.regexp/regexp-gen/javadoc .svg?style=plastic )] ( https://javadoc.io/doc/org.cornutum.regexp/regexp-gen )
3
+ [ ![ Maven] ( https://img.shields.io/badge/ maven-2.0.0-green .svg )] ( https://search. maven.org/search?q= regexp-gen )
4
+ [ ![ Javadoc ] ( https://img.shields. io/badge/javadoc-2.0.0-green .svg )] ( https://javadoc.io/doc/org.cornutum.regexp/regexp-gen/latest/index.html )
5
5
6
6
## Contents ##
7
7
10
10
* [ How Does It Work?] ( #how-does-it-work )
11
11
* [ The basics] ( #the-basics )
12
12
* [ Exact matches vs. substring matches] ( #exact-matches-vs-substring-matches )
13
+ * [ Want strings that _ DON'T_ match?] ( #want-strings-that-dont-match )
13
14
* [ What matches "dot"?] ( #what-matches-dot )
14
15
* [ How to create longer matching strings] ( #how-to-create-longer-matching-strings )
15
16
* [ How to generate random matches repeatably] ( #how-to-generate-random-matches-repeatably )
18
19
19
20
## What's New? ##
20
21
21
- * The latest version ([ 1.2.4 ] ( https://github.com/Cornutum/regexp-gen/releases/tag/release-1.2.4 ) )
22
+ * The latest version ([ 2.0.0 ] ( https://github.com/Cornutum/regexp-gen/releases/tag/release-2.0.0 ) )
22
23
is now available at the [ Maven Central Repository] ( https://search.maven.org/search?q=regexp-gen ) .
23
24
24
25
## What Is It? ##
@@ -37,7 +38,7 @@ Here's a simple example of how to use a `RegExpGen`.
37
38
``` java
38
39
import org.cornutum.regexpgen.RandomGen ;
39
40
import org.cornutum.regexpgen.RegExpGen ;
40
- import org.cornutum.regexpgen.js.Parser ;
41
+ import org.cornutum.regexpgen.js.Provider ;
41
42
import org.cornutum.regexpgen.random.RandomBoundsGen ;
42
43
43
44
// Given a JavaScript regular expression...
@@ -47,7 +48,7 @@ String regexp = "^Regular expressions are ((odd|hard|stupid), )+but cool!$";
47
48
RandomGen random = new RandomBoundsGen ();
48
49
49
50
// ...create a RegExpGen instance...
50
- RegExpGen generator = Parser . parseRegExp ( regexp);
51
+ RegExpGen generator = Provider . forEcmaScript() . matching ( regexp);
51
52
52
53
for ( int i = 0 ; i < 3 ; i++ )
53
54
{
@@ -75,7 +76,7 @@ least one substring that matches". For example, consider what happens when you r
75
76
String regexp = " (Hello|Howdy|Allô), world!" ;
76
77
77
78
// ...create a RegExpGen instance...
78
- RegExpGen generator = Parser . parseRegExp ( regexp);
79
+ RegExpGen generator = Provider . forEcmaScript() . matching ( regexp);
79
80
...
80
81
```
81
82
@@ -92,15 +93,15 @@ These strings contain not only a substring that matches the regular expression,
92
93
general "substring match", that's exactly what you want to give it.
93
94
94
95
But what if you need to generate only "exact" matches? In other words, strings containing only the matching characters.
95
- To do that, use ` Parser.parseRegExpExact ()` .
96
+ To do that, use ` Provider.matchingExact ()` .
96
97
97
98
``` java
98
99
...
99
100
// Given a JavaScript regular expression...
100
101
String regexp = " (Hello|Howdy|Allô), world!" ;
101
102
102
103
// ...create a RegExpGen instance...
103
- RegExpGen generator = Parser . parseRegExpExact ( regexp);
104
+ RegExpGen generator = Provider . forEcmaScript() . matchingExact ( regexp);
104
105
...
105
106
```
106
107
@@ -112,6 +113,35 @@ Allô, world!
112
113
Howdy, world!
113
114
```
114
115
116
+ ### Want strings that _ DON'T_ match? ###
117
+
118
+ You can also create a ` RegExpGen ` that generates strings that _ don't_ match a specified regular expression. This is handy when
119
+ you're testing a system that applies a regular expression and you want to see what happens when it gets invalid input. Here's how to do it.
120
+
121
+ ``` java
122
+ ...
123
+ // Given a JavaScript regular expression...
124
+ String regexp = " (Hello|Howdy|Allô), world!" ;
125
+
126
+ // ...create a RegExpGen instance...
127
+ RegExpGen generator =
128
+ Provider . forEcmaScript(). notMatching( regexp)
129
+ .orElseThrow( () - > new IllegalStateException ( String . format( " Unable to generate string not matching '%s'" , regexp)));
130
+
131
+ ...
132
+ ```
133
+
134
+ Run with this change and the result will look like this:
135
+
136
+ ```
137
+ HHHAA
138
+ 3)!)5!':<
139
+ "+#6)!%;)*8/ 28
140
+ ```
141
+
142
+ Note that ` Provider.notMatching() ` returns an optional result. Why? Because some regular expressions will match any string, making it
143
+ impossible to generate strings that don't match. For example, there is no string that doesn't match ` ".*" ` .
144
+
115
145
### What matches "dot"? ###
116
146
117
147
What matches the regular expression ` . ` ? In general, any printable character. For a ` RegExpGen ` , by default, that means "any printable character
@@ -129,7 +159,7 @@ String regexp = regexp( "<< My secret is [^\\d\\s]{8,32} >>");
129
159
RandomGen random = getRandomGen();
130
160
131
161
// ...create a RegExpGen instance...
132
- RegExpGen generator = Parser . parseRegExp ( regexp);
162
+ RegExpGen generator = Provider . forEcmaScript() . matching ( regexp);
133
163
134
164
// ...matching "." with specific characters...
135
165
generator. getOptions(). setAnyPrintableChars( " 1001 Anagrams!" );
@@ -283,9 +313,9 @@ matches will always lie between the given limits. Instead, `RegExpGen` makes a s
283
313
* **What flavor of regular expression syntax is supported?**
284
314
285
315
The `RegExpGen` interface is designed to allow implementations for different regular expression
286
- engines. The current version provides an implementation for JavaScript `RegExp` (specifically,
287
- as defined by the [ECMAScript](https ://www.ecma-international .org/ecma-262/#sec-patterns )
288
- standard).
316
+ engines. The current version provides an
317
+ [implementation for JavaScript `RegExp`](http ://www.cornutum .org/regexp-gen/apidocs/org/cornutum/regexpgen/js/Provider.html )
318
+ (specifically, as defined by the [ECMAScript](https://www.ecma-international.org/ecma-262/#sec-patterns) standard).
289
319
290
320
Note that the syntax for the Java `Pattern` implementation overlaps quite a bit with `RegExp`,
291
321
so this `RegExpGen` implementation can also be used to generate matches for most Java `Pattern`
@@ -306,7 +336,7 @@ matches will always lie between the given limits. Instead, `RegExpGen` makes a s
306
336
307
337
* **Now that I've created a `RegExpGen`, how do I know the regular expression it's generating matches for?**
308
338
309
- Easy -- just call `RegExpGen.getOptions().getRegExp ()`.
339
+ Easy -- just call `RegExpGen.getSource ()`.
310
340
311
341
* **Hey, what happened to release version 1.2.2?**
312
342
0 commit comments