Skip to content

Commit bc1e0ba

Browse files
committed
v1.1
1 parent af64636 commit bc1e0ba

20 files changed

+1088
-456
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ build/
3232
.vscode/
3333

3434
### Mac OS ###
35-
.DS_Store
35+
.DS_Store
36+
37+
.env

README.md

Lines changed: 98 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
[Homepage](https://regexsolver.com) | [Online Demo](https://regexsolver.com/demo) | [Documentation](https://docs.regexsolver.com) | [Developer Console](https://console.regexsolver.com)
44

5-
This repository contains the source code of the Java library for [RegexSolver](https://regexsolver.com) API.
6-
7-
RegexSolver is a powerful regular expression manipulation toolkit, that gives you the power to manipulate regex as if
8-
they were sets.
5+
**RegexSolver** is a powerful toolkit for building, combining, and analyzing regular expressions. It is designed for constraint solvers, test generators, and other systems that need advanced regex operations.
96

107
## Installation
118

@@ -19,205 +16,154 @@ they were sets.
1916
<dependency>
2017
<groupId>com.regexsolver.api</groupId>
2118
<artifactId>RegexSolver</artifactId>
22-
<version>1.0.2</version>
19+
<version>1.1.0</version>
2320
</dependency>
2421
```
2522

2623
### Gradle
2724

2825
```groovy
29-
implementation "com.regexsolver.api:RegexSolver:1.0.2"
26+
implementation "com.regexsolver.api:RegexSolver:1.1.0"
3027
```
3128

3229
## Usage
3330

34-
In order to use the library you need to generate an API Token on
35-
our [Developer Console](https://console.regexsolver.com/).
36-
37-
```java
38-
import com.regexsolver.api.RegexSolver;
39-
import com.regexsolver.api.Term;
40-
import com.regexsolver.api.exception.ApiError;
41-
42-
import java.io.IOException;
43-
44-
public class Main {
45-
public static void main(String[] args) throws IOException, ApiError {
46-
RegexSolver.initialize("YOUR TOKEN HERE");
47-
48-
Term term1 = Term.regex("(abc|de|fg){2,}");
49-
Term term2 = Term.regex("de.*");
50-
Term term3 = Term.regex(".*abc");
51-
52-
Term term4 = Term.regex(".+(abc|de).+");
53-
54-
Term result = term1.intersection(term2, term3)
55-
.subtraction(term4);
56-
57-
System.out.println(result);
58-
}
59-
}
60-
```
61-
62-
## Features
63-
64-
- [Intersection](#intersection)
65-
- [Union](#union)
66-
- [Subtraction / Difference](#subtraction--difference)
67-
- [Equivalence](#equivalence)
68-
- [Subset](#subset)
69-
- [Details](#details)
70-
- [Generate Strings](#generate-strings)
71-
72-
### Intersection
73-
74-
#### Request
75-
76-
Compute the intersection of the provided terms and return the resulting term.
77-
78-
The maximum number of terms is currently limited to 10.
31+
1. Create an API token in the [Developer Console](https://console.regexsolver.com/).
32+
2. Initialize the client and start working with terms:
7933

8034
```java
81-
Term.Regex term1 = Term.regex("(abc|de){2}");
82-
Term.Regex term2 = Term.regex("de.*");
83-
Term.Regex term3 = Term.regex(".*abc");
35+
// Set REGEXSOLVER_API_TOKEN in your env and call initialize(),
36+
// or pass the token directly:
37+
RegexSolver.initialize(); // or RegexSolver.initialize("YOUR_API_TOKEN");
8438

85-
Term result = term1.intersection(term2, term3);
86-
System.out.println(result);
87-
```
88-
89-
#### Response
39+
Term term1 = Term.regex("(abc|de|fg){2,}");
40+
Term term2 = Term.regex("de.*");
41+
Term term3 = Term.regex(".*abc");
9042

91-
```
92-
regex=deabc
43+
Term result = term1.intersection(term2, term3)
44+
.difference(Term.regex(".+(abc|de).+"));
45+
System.out.println(result.getPattern()); // de(fg)*abc
9346
```
9447

95-
### Union
9648

97-
Compute the union of the provided terms and return the resulting term.
98-
99-
The maximum number of terms is currently limited to 10.
100-
101-
#### Request
102-
103-
```java
104-
Term.Regex term1 = Term.regex("abc");
105-
Term.Regex term2 = Term.regex("de");
106-
Term.Regex term3 = Term.regex("fghi");
107-
108-
Term result = term1.union(term2, term3);
109-
System.out.println(result);
110-
```
49+
## Key Concepts & Limitations
11150

112-
#### Response
51+
RegexSolver supports a subset of regular expressions that adhere to the principles of regular languages. Here are the key characteristics and limitations of the regular expressions supported by RegexSolver:
52+
- **Anchored Expressions:** All regular expressions in RegexSolver are anchored. This means that the expressions are treated as if they start and end at the boundaries of the input text. For example, the expression `abc` will match the string "abc" but not "xabc" or "abcx".
53+
- **Lookahead/Lookbehind:** RegexSolver does not support lookahead (`(?=...)`) or lookbehind (`(?<=...)`) assertions. Using them returns an error.
54+
- **Pure Regular Expressions:** RegexSolver focuses on pure regular expressions as defined in regular language theory. This means features that extend beyond regular languages, such as backreferences (`\1`, `\2`, etc.), are not supported. Any use of backreference would return an error.
55+
- **Greedy/Ungreedy Quantifiers:** The concept of ungreedy (`*?`, `+?`, `??`) quantifiers is not supported. All quantifiers are treated as greedy. For example, `a*` or `a*?` will match the longest possible sequence of "a"s.
56+
- **Line Feed and Dot:** RegexSolver handles all characters the same way. The dot `.` matches any Unicode character including line feed (`\n`).
57+
- **Empty Regular Expressions:** The empty language (matches no string) is represented by constructs like `[]` (empty character class). This is distinct from the empty string.
11358

114-
```
115-
regex=(abc|de|fghi)
116-
```
11759

118-
### Subtraction / Difference
60+
## Response Formats
11961

120-
Compute the first term minus the second and return the resulting term.
62+
The API can handle terms in two formats:
63+
- `regex`: a regular expression pattern
64+
- `fair`: FAIR (Fast Automaton Internal Representation), a stable, signed format used internally by the engine
12165

122-
#### Request
66+
By default, the engine returns whatever the operation produces, with no extra convertion. Override with `responseFormat`:
12367

12468
```java
125-
Term.Regex term1 = Term.regex("(abc|de)");
126-
Term.Regex term2 = Term.regex("de");
127-
128-
Term result = term1.subtraction(term2);
129-
System.out.println(result);
130-
```
69+
import com.regexsolver.Term;
70+
import com.regexsolver.ResponseFormat;
13171

132-
#### Response
133-
134-
```
135-
regex=abc
136-
```
72+
Term term = Term.regex("abcde");
13773

138-
### Equivalence
74+
OperationOptions operationOptions = OperationOptions.init()
75+
.responseFormat(ResponseFormat.REGEX);
76+
Term result1 = term.union(operationOptions, Term.regex("de"));
13977

140-
Analyze if the two provided terms are equivalent.
78+
System.out.println(result1.toString()); // regex=(abc)?de
14179

142-
#### Request
80+
operationOptions = OperationOptions.init()
81+
.responseFormat(ResponseFormat.FAIR);
82+
Term result2 = term.intersection(operationOptions, Term.regex("de.*"));
14383

144-
```java
145-
Term.Regex term1 = Term.regex("(abc|de)");
146-
Term.Fair term2 = Term.regex("(abc|de)*");
147-
148-
boolean result = term1.isEquivalentTo(term2);
149-
System.out.println(result);
84+
System.out.println(r2.toString()); // fair=...
15085
```
15186

152-
#### Response
153-
154-
```
155-
false
156-
```
87+
If the format does not matter, omit `responseFormat` or set it to `ResponseFormat.ANY`.
15788

158-
### Subset
89+
Regardless of the format, you can always call `getPattern()` to obtain the regex pattern of a term.
15990

160-
Analyze if the second term is a subset of the first.
91+
## Bounding execution time
16192

162-
#### Request
93+
Set a server-side compute timeout in milliseconds with `executionTimeout`:
16394

16495
```java
165-
Term.Regex term1 = Term.regex("de");
166-
Term.Regex term2 = Term.regex("(abc|de)");
167-
168-
boolean result = term1.isSubsetOf(term2);
169-
System.out.println(result);
96+
import com.regexsolver.ApiError;
97+
import com.regexsolver.Term;
98+
99+
try {
100+
Term out = Term.regex(".*ab.*c(de|fg).*dab.*c(de|fg).*ab.*c(de|fg).*dab.*c")
101+
.difference(Term.regex(".*abc.*"));
102+
} catch (ApiError e) {
103+
System.out.println(e.getMessage()); // The operation took too much time.
104+
}
170105
```
171106

172-
#### Response
107+
Timeout is best effort. The exact time is not guaranteed.
173108

174-
```
175-
true
176-
```
109+
## API Overview
177110

178-
### Details
111+
`Term` exposes the following methods.
179112

180-
Compute the details of the provided term.
113+
### Build
114+
| Method | Return | Description |
115+
| -------- | ------- | ------- |
116+
| `Term.fair(String fair)` | `Term` | Creates a term from a FAIR. |
117+
| `Term.regex(String regex)` | `Term` | Creates a term from a regex pattern. |
181118

182-
The computed details are:
119+
### Analyze
183120

184-
- **Cardinality:** the number of possible values.
185-
- **Length:** the minimum and maximum length of possible values.
186-
- **Empty:** true if is an empty set (does not contain any value), false otherwise.
187-
- **Total:** true if is a total set (contains all values), false otherwise.
121+
| Method | Return | Description |
122+
| -------- | ------- | ------- |
123+
| `t.equivalent(Term term)` | `boolean` | `true` if `t` and `term` accept exactly the same language. Supports `executionTimeout`. |
124+
| `t.getCardinality()` | `Cardinality` | Returns the cardinality of the term (i.e., the number of possible matched strings). |
125+
| `t.getDetails()` | `Details` | Returns cardinality, length bounds, and if it is empty or total. |
126+
| `t.getDot()` | `String` | Returns a Graphviz DOT representation of the automaton for the term. |
127+
| `t.getFair()` | `String` | Returns the FAIR of the term if defined. |
128+
| `t.getLength()` | `Length` | Returns the minimum and maximum length of matched strings. |
129+
| `t.getPattern()` | `String` | Returns a regular expression pattern for the term. |
130+
| `t.isEmpty()` | `boolean` | `true` if the term matches no string. |
131+
| `t.isEmptyString()` | `boolean` | `true` if the term matches only the empty string. |
132+
| `t.isTotal()` | `boolean` | `true` if the term matches all possible strings. |
133+
| `t.subset(Term term)` | `boolean` | `true` if every string matched by `t` is also matched by `term`. Supports `executionTimeout`. |
188134

189-
#### Request
135+
### Compute
190136

191-
```java
192-
Term.Regex term = Term.regex("(abc|de)");
137+
| Method | Return | Description |
138+
| -------- | ------- | ------- |
139+
| `t.concat(Term... terms)` | `Term` | Concatenates `t` with the given terms. Supports `responseFormat` and `executionTimeout`. |
140+
| `t.difference(Term term)` | `Term` | Computes the difference `t - term`. Supports `responseFormat` and `executionTimeout`. |
141+
| `t.intersection(Term... terms)` | `Term` | Computes the intersection of `t` with the given terms. Supports `responseFormat` and `executionTimeout`. |
142+
| `t.repeat(int min, Integer max)` | `Term` | Computes the repetition of the term between `min` and `max` times; if `max` is `null`, the repetition is unbounded. Supports `responseFormat` and `executionTimeout`. |
143+
| `t.union(Term... terms)` | `Term` | Computes the union of `t` with the given terms. Supports `responseFormat` and `executionTimeout`. |
193144

194-
Details details = term.getDetails();
195-
System.out.println(details);
196-
```
145+
### Generate
197146

198-
#### Response
147+
| Method | Return | Description |
148+
| -------- | ------- | ------- |
149+
| `t.generateStrings(int count)` | `String[]` | Generates up to `count` unique example strings matched by `t`. Supports `executionTimeout`. |
199150

200-
```
201-
Details[cardinality=Integer(2), length=Length[minimum=2, maximum=3], empty=false, total=false]
202-
```
151+
### Other
152+
| Method | Return | Description |
153+
| -------- | ------- | ------- |
154+
| `t.serialize()` | `String` | Returns a serialized form of `t`. |
155+
| `Term.deserialize(String string)` | `Term` | Returns a deserialized term from the given `string`. |
203156

204-
### Generate Strings
157+
## Cross-Language Support
205158

206-
Generate the given number of strings that can be matched by the provided term.
159+
If you want to use this library with other programming languages, we provide:
160+
- [regexsolver-js](https://github.com/RegexSolver/regexsolver-js)
161+
- [regexsolver-python](https://github.com/RegexSolver/regexsolver-python)
207162

208-
The maximum number of strings to generate is currently limited to 200.
163+
For more information about how to use the wrappers, you can refer to our [guide](https://docs.regexsolver.com/getting-started.html).
209164

210-
#### Request
165+
You can also take a look at [regexsolver](https://github.com/RegexSolver/regexsolver) which contains the source code of the engine.
211166

212-
```java
213-
Term.Regex term = Term.regex("(abc|de){2}");
214-
215-
List<String> strings = term.generateStrings(3);
216-
System.out.println(strings);
217-
```
167+
## License
218168

219-
#### Response
220-
221-
```
222-
[abcde, dede, deabc]
223-
```
169+
This project is licensed under the MIT License.

pom.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.regexsolver.api</groupId>
88
<artifactId>RegexSolver</artifactId>
9-
<version>1.0.2</version>
9+
<version>1.1.0</version>
1010

1111
<url>https://regexsolver.com</url>
1212
<organization>
@@ -17,8 +17,7 @@
1717
<url>https://github.com/RegexSolver/regexsolver-java</url>
1818
</scm>
1919
<description>
20-
RegexSolver allows you to manipulate regular expressions as sets, enabling operations such as intersection,
21-
union, and subtraction.
20+
RegexSolver is a powerful toolkit for building, combining, and analyzing regular expressions.
2221
</description>
2322
<developers>
2423
<developer>
@@ -44,12 +43,12 @@
4443
<dependency>
4544
<groupId>com.squareup.retrofit2</groupId>
4645
<artifactId>retrofit</artifactId>
47-
<version>2.11.0</version>
46+
<version>2.12.0</version>
4847
</dependency>
4948
<dependency>
5049
<groupId>com.squareup.retrofit2</groupId>
5150
<artifactId>converter-jackson</artifactId>
52-
<version>2.11.0</version>
51+
<version>2.12.0</version>
5352
</dependency>
5453
<dependency>
5554
<groupId>junit</groupId>

src/main/java/com/regexsolver/api/RegexSolver.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.regexsolver.api;
22

33
public final class RegexSolver {
4+
public static void initialize() {
5+
RegexSolverApiWrapper.initialize();
6+
}
7+
48
public static void initialize(String token) {
59
RegexSolverApiWrapper.initialize(token);
610
}

0 commit comments

Comments
 (0)