# Union, Intersection, Difference and Complement

These operations build a new Term from existing ones using classic **set operations**. They are the core of the API, and every result is a Term you can pass to the next call.

## Union

Returns a Term representing strings that appear in **any of the provided Terms** (logical OR).

Example:

<CodeTabs defaultValue="java">
<CodeTabs.Tab value="java" label="Java">
```java
RegexSolverClient client = RegexSolverClient.builder()
    .apiToken(System.getenv("REGEXSOLVER_API_TOKEN"))
    .build();

Term a = Term.regex("abe");
Term b = Term.regex("cde");
Term c = Term.regex("efe");

OperationOptions options = OperationOptions.builder()
        .responseFormat(ResponseFormat.REGEX);

Term result = client.union(List.of(a, b, c), options);

System.out.println(result); // regex=(ab|cd|ef)e
```
</CodeTabs.Tab>

<CodeTabs.Tab value="javascript" label="JavaScript">
```javascript
const client = new RegexSolverClient({ apiToken: process.env.REGEXSOLVER_API_TOKEN });

const a = Term.regex("abe");
const b = Term.regex("cde");
const c = Term.regex("efe");

const result = await client.union(a, b, c, { responseFormat: ResponseFormat.REGEX });

console.log(result); // regex=(ab|cd|ef)e
```
</CodeTabs.Tab>

<CodeTabs.Tab value="python" label="Python">
```python
client = RegexSolverClient(api_token=os.getenv("REGEXSOLVER_API_TOKEN"))

a = Term.regex(r"abe")
b = Term.regex(r"cde")
c = Term.regex(r"efe")

result = client.union(a, b, c, response_format=ResponseFormat.REGEX)

print(result) # regex=(ab|cd|ef)e
```
</CodeTabs.Tab>
</CodeTabs>

## Intersection

Returns a Term representing only strings that appear in **all of the provided Terms** (logical AND).

Example:

<CodeTabs defaultValue="java">
<CodeTabs.Tab value="java" label="Java">
```java
RegexSolverClient client = RegexSolverClient.builder()
    .apiToken(System.getenv("REGEXSOLVER_API_TOKEN"))
    .build();

Term a = Term.regex("[a-z]{2}");
Term b = Term.regex("(ab|cd|efg)");
Term c = Term.regex("a.*");

OperationOptions options = OperationOptions.builder()
        .responseFormat(ResponseFormat.REGEX);

Term result = client.intersection(List.of(a, b, c), options);

System.out.println(result); // regex=ab
```
</CodeTabs.Tab>

<CodeTabs.Tab value="javascript" label="JavaScript">
```javascript
const client = new RegexSolverClient({ apiToken: process.env.REGEXSOLVER_API_TOKEN });

const a = Term.regex("[a-z]{2}");
const b = Term.regex("(ab|cd|efg)");
const c = Term.regex("a.*");

const result = await client.intersection(a, b, c, { responseFormat: ResponseFormat.REGEX });

console.log(result); // regex=ab
```
</CodeTabs.Tab>

<CodeTabs.Tab value="python" label="Python">
```python
client = RegexSolverClient(api_token=os.getenv("REGEXSOLVER_API_TOKEN"))

a = Term.regex(r"[a-z]{2}")
b = Term.regex(r"(ab|cd|efg)")
c = Term.regex(r"a.*")

result = client.intersection(a, b, c, response_format=ResponseFormat.REGEX)

print(result) # regex=ab
```
</CodeTabs.Tab>
</CodeTabs>

## Difference

Returns a Term representing strings that are in the **first Term** but **not in the second one**.

Example:

<CodeTabs defaultValue="java">
<CodeTabs.Tab value="java" label="Java">
```java
RegexSolverClient client = RegexSolverClient.builder()
    .apiToken(System.getenv("REGEXSOLVER_API_TOKEN"))
    .build();

Term a = Term.regex("(cat|dog|owl)");
Term b = Term.regex("dog");

OperationOptions options = OperationOptions.builder()
        .responseFormat(ResponseFormat.REGEX);

Term result = client.difference(a, b, options);

System.out.println(result); // regex=(cat|owl)
```
</CodeTabs.Tab>

<CodeTabs.Tab value="javascript" label="JavaScript">
```javascript
const client = new RegexSolverClient({ apiToken: process.env.REGEXSOLVER_API_TOKEN });

const a = Term.regex("(cat|dog|owl)");
const b = Term.regex("dog");

const result = await client.difference(a, b, { responseFormat: ResponseFormat.REGEX });

console.log(result); // regex=(cat|owl)
```
</CodeTabs.Tab>

<CodeTabs.Tab value="python" label="Python">
```python
client = RegexSolverClient(api_token=os.getenv("REGEXSOLVER_API_TOKEN"))

a = Term.regex(r"(cat|dog|owl)")
b = Term.regex(r"dog")

result = client.difference(a, b, response_format=ResponseFormat.REGEX)

print(result) # regex=(cat|owl)
```
</CodeTabs.Tab>
</CodeTabs>

## Complement

Returns a Term representing every string that is **not** in the given Term's language.

Example:

<CodeTabs defaultValue="java">
<CodeTabs.Tab value="java" label="Java">
```java
RegexSolverClient client = RegexSolverClient.builder()
    .apiToken(System.getenv("REGEXSOLVER_API_TOKEN"))
    .build();

Term term = Term.regex(".*a.*");

OperationOptions options = OperationOptions.builder()
        .responseFormat(ResponseFormat.REGEX);

Term result = client.complement(term, options);

System.out.println(result); // regex=[^a]*
```
</CodeTabs.Tab>

<CodeTabs.Tab value="javascript" label="JavaScript">
```javascript
const client = new RegexSolverClient({ apiToken: process.env.REGEXSOLVER_API_TOKEN });

const term = Term.regex(".*a.*");

const result = await client.complement(term, { responseFormat: ResponseFormat.REGEX });

console.log(result); // regex=[^a]*
```
</CodeTabs.Tab>

<CodeTabs.Tab value="python" label="Python">
```python
client = RegexSolverClient(api_token=os.getenv("REGEXSOLVER_API_TOKEN"))

term = Term.regex(r".*a.*")

result = client.complement(term, response_format=ResponseFormat.REGEX)

print(result) # regex=[^a]*
```
</CodeTabs.Tab>
</CodeTabs>
