Skip to content

Add text.replace, text.regReplace, text.distance functions #1321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion pages/advanced-algorithms/available-algorithms/text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ If subgraph is not specified, the algorithm is computed on the entire graph by d

{<h4 className="custom-header"> Usage: </h4>}

Use the following query to insert the parameters to the placeholders in the sentence:
Use the following queries to insert the parameters to the placeholders in the sentence:

```cypher
CALL text.format("Memgraph is the number {} {} in the world.", [1, "graph database"])
Expand All @@ -137,3 +137,106 @@ Result:
| "Memgraph is the number 1 graph database in the world. "|
+---------------------------------------------------------+
```

### `replace()`

Replace each substring of the given string that matches the given regular expression with the given replacement.

{<h4 className="custom-header"> Input: </h4>}

- `subgraph: Graph` (**OPTIONAL**) ➡ A specific subgraph, which is an [object of type Graph](/advanced-algorithms/run-algorithms#run-procedures-on-subgraph) returned by the `project()` function, on which the algorithm is run.
If subgraph is not specified, the algorithm is computed on the entire graph by default.
- `text: string` ➡ Text that needs to be replaced.
- `regex: string` ➡ Regular expression by which to replace the string.
- `replacement: string` ➡ Target string to replace the matched string.

{<h4 className="custom-header"> Usage: </h4>}

Use the following queries to do text replacement:

```cypher
RETURN text.replace('Hello World!', '[^a-zA-Z]', '') AS result;
```

Result:

```plaintext
+--------------+
| result |
+--------------+
| "HelloWorld" |
+--------------+
```

```cypher
RETURN text.replace('MAGE is a Memgraph Product', 'MAGE', 'GQLAlchemy') AS result;
```

Result:

```plaintext
+------------------------------------+
| result |
+---------- -------------------------+
| "GQLAlchemy is a Memgraph Product" |
+------------------------------------+
```

### `regReplace()`

Replace each substring of the given string that matches the given regular expression with the given replacement.

{<h4 className="custom-header"> Input: </h4>}

- `subgraph: Graph` (**OPTIONAL**) ➡ A specific subgraph, which is an [object of type Graph](/advanced-algorithms/run-algorithms#run-procedures-on-subgraph) returned by the `project()` function, on which the algorithm is run.
If subgraph is not specified, the algorithm is computed on the entire graph by default.
- `text: string` ➡ Text that needs to be replaced.
- `regex: string` ➡ Regular expression by which to replace the string.
- `replacement: string` ➡ Target string to replace the matched string.

{<h4 className="custom-header"> Usage: </h4>}

Use the following query to do text replacement:

```cypher
RETURN text.regreplace("Memgraph MAGE Memgraph MAGE", "MAGE", "GQLAlchemy") AS output;
```

Result:

```plaintext
+---------------------------------------+
| result |
+---------------------------------------+
| "GQLAlchemy MAGE Memgraph GQLAlchemy" |
+---------------------------------------+
```

### `distance()`

Compare the given strings with the Levenshtein distance algorithm.

{<h4 className="custom-header"> Input: </h4>}

- `subgraph: Graph` (**OPTIONAL**) ➡ A specific subgraph, which is an [object of type Graph](/advanced-algorithms/run-algorithms#run-procedures-on-subgraph) returned by the `project()` function, on which the algorithm is run.
If subgraph is not specified, the algorithm is computed on the entire graph by default.
- `text1: string` ➡ Source string.
- `text2: string` ➡ Destination string for comparison.

{<h4 className="custom-header"> Usage: </h4>}

Use the following query to calculate distance between texts:

```cypher
RETURN text.distance("Levenshtein", "Levenstein") AS result;
```

Result:

```plaintext
+--------+
| result |
+--------+
| 1 |
+--------+
```