# Regex

The Regex manipulator is able to use regex to search and replace matches or parts of a match separated into groups. The regex manipulator also has support for replacing IDs using Impulse's ID maps.

Nearly any regex string can be used to find a match to replace.

## Properties

| Property               | Purpose                                                         | Required |
| ---------------------- | --------------------------------------------------------------- | -------- |
| fieldName              | The field name to apply regex to.                               | True     |
| regex                  | Regex to apply                                                  | True     |
| replace                | Replacement string                                              | False    |
| delim                  | Delimiter to parse regex with                                   | False    |
| sourceContentType      | The source content type to search for a mapped ID with          | False    |
| destinationContentType | The destination content type to search for a mapped ID with     | False    |
| regexLanguage          | The language of regex to run expression with                    | False    |
| destinationEndpointId  | Destination Endpoint ID to use when manipulator used "on write" | False    |

## Specifics

### How to use

The `regex` and `replace` properties each take a string value and are required to use the Regex manipulator.

The `regex` value is the value to search for and the `replace` value is the value that should replace what the `regex` value matches.

#### ID Maps

The Regex manipulator can be used to group a value and replace the value with an ID map from Impulse.

The `replace` value must start and end with a pair of curly brackets `{}` . Inside the curly brackets the path of the ID to replace must be set. i.e., `{content.id}` or `{content.version.id}`

#### delimiter

Comma is used as the default delimiter for the `replace` value. The replace value can be separated when groups need to be individually replaced with values. This allows for using groups and ID maps to replace segments of a match with different values.

{% hint style="info" %}
Note: The `replace` value uses `,` as a default delimiter. If you require the replace value to include a comma the `delim` propery must be set with a different value.
{% endhint %}

#### Groups

Groups can be used in the `regex` value . When using groups each group can be set in the `replace` value by using ${#} notation. i.e., $1 will refer to the first group. $3 will refer to the third group.

When using groups in `regex` value, use a delimiter to specify how to replace each group in the `replace` value. the group number directly relates to the segment number broken by delimiter.

i.e., regex is used to create 3 groups. `replace` value should use 2 delimiters.

```
regex: (group1)(group2)(group3)
replace: replaceGroup1,${2},replaceGroup3
```

#### Source/Destination Content Type

These options should be used when the `content.id` being replaced has been synced to multiple destination content types. Meaning the single source content was synced as multiple destination contents with different content types.&#x20;

In this case, there is no guarantee what content type would be used to find an ID map. To guarantee what content type to use, set both the `sourceContentType` and `destinationContentType` .&#x20;

This is only required when the `content.id` map being searched for was previously synced as multiple destination contents with different content types. If the `content.id` was only synced to a single content type at the destination, then these config options are not required.&#x20;

Details about the content types used can be found [here](https://motivlabs.gitbook.io/impulse-user-manual/getting-started/content-mapper/aligning-mismatched-content#default-content-types).&#x20;

### Regex Language

The following languages can be used for the provided regex.&#x20;

* PCRE2
* RE2

Based on the selected Regex language, not all functionality may be available.&#x20;

### RE2

This is the default language used.&#x20;

The RE2 language has full support for all functionality of the regex manipulator. It's limitations are the limitations of the RE2 language. Such as no lookahead/lookbehind support.&#x20;

### PCRE2

The PCRE2 langauge only supports global search and replace. Meaning that every match found will be replaced.&#x20;

Because it only supports global search and replace functionality such as ID Maps and select group replacements are not supported.&#x20;

{% hint style="info" %}
The regex string automatically prepends "/" and appends "/m" before running, so these should not be added to the regex string unless they are part of the actual regex to search with.&#x20;
{% endhint %}

## Examples

### Basic search and replace

This example shows using a simple regex to search and replace each instance with a simple value.

Assume your initial value is as follows.

```
Special string to replace with regex string.
```

You now want to replace each occurrence of "string" with "text". You can do this by setting the following values.

```
regex: string
replace: text
```

You'd then have this final value.

```
Special text to replace with regex text.
```

### Replacing URL paths to files (RE2 only)

This example shows how groups and ID maps can be used together with a delimiter to replace the URL path for files that have been previously migrated via an Impulse sync.

The problem is that the asset migrated to the destination now has a new file path because its IDs have changed.

Assume you have the following value.

{% code overflow="wrap" %}

```
\"https://eu-images.contentstack.com/v3/assets/blt8770191dea35dcba/blt1a357ea837cf4406/644a77959c28f2c0649a195e/Amber.jpg\"
```

{% endcode %}

The following values in the URL need to be replaced as follows.

```
blt8770191dea35dcba -> blt69509c911644abcd
blt1a357ea837cf4406 -> {content.id}
644a77959c28f2c0649a195e -> {content.version.id}
Amber.jpg" -> Amber.jpg?branch=migration_poc"
```

This can be accomplished by using the delimiter in the `replace` value and combing the use of groups and ID map support. The following regex and replace value can be used.

{% code overflow="wrap" %}

```
regex: (\/v3\/assets\/)(.*?)(\/)(.*?)(\/)(.*?)(\/.*?)(\?.*?\"|\")
replace: ${1},blt69509c911644abcd,${3},{content.id},${5},{content.version.id},${7},?branch=migration_poc"
```

{% endcode %}

The `regex` value creates a match for URL from `/v3` to `\"` . 8 groups are used to segment the entire match so each segment can be individually replaced or kept.

In the `replace` value, a comma is used as a delimiter to specify when a static value is used, a group is used or an ID map value is used for every group created in the regex.

* Groups 1, 3, 5, and 7 have all been kept the same and in the same place.
* Group 2 was replaced with the static value `blt69509c911644abcd`.
* Group 4 was replaced with the ID map value found for {content.id}. In this case, `blt9cd3e9a81dd9315a`.
* Group 6 was replaced with the ID map value found for {content.version.id}. In this case,`645506c0a5217e70d3f9a3d2`.
* Group 8 was replaced with the static value `?branch=migration_poc"`

Using these configuration values, the URL can then become the following value.

{% code overflow="wrap" %}

```
\"https://eu-images.contentstack.com/v3/assets/blt69509c911644abcd/blt9cd3e9a81dd9315a/645506c0a5217e70d3f9a3d2/Amber.jpg?branch=migration_poc\"
```

{% endcode %}

Each group specified has been kept or replaced.

This example can be expanded for multiple matches.

Assume you have an HTML value with `img` tags that have a path to an asset that was previously migrated to the destination via an Impulse sync. The value may look something like the following

{% code overflow="wrap" %}

```
<p>some linked image </p><img asset_uid=\"blt1a357ea837cf4406\" src=\"https://eu-images.contentstack.com/v3/assets/blt8770191dea35dcba/blt1a357ea837cf4406/644a77959c28f2c0649a195e/Amber.jpg\"/><p></p><p></p><img asset_uid=\"blt6459005bb313b6a5\" src=\"https://eu-images.contentstack.com/v3/assets/blt8770191dea35dcba/blt6459005bb313b6a5/644a77955a39ff1e4941af43/Mixtape-Contributor.png\" height=\"auto\"/>
```

{% endcode %}

Using the same config, the following value can be created.

{% code overflow="wrap" %}

```
<p>some linked image </p><img asset_uid=\"blt9cd3e9a81dd9315a\" src=\"https://eu-images.contentstack.com/v3/assets/blt69509c911644abcd/blt9cd3e9a81dd9315a/645506c0a5217e70d3f9a3d2/Amber.jpg?branch=migration_poc\"/><p></p><p></p><img asset_uid=\"blt9789501b5bb09487\" src=\"https://eu-images.contentstack.com/v3/assets/blt69509c911644abcd/blt9789501b5bb09487/645506bf74c5142ac49d4895/Mixtape-Contributor.png?branch=migration_poc\" height=\"auto\"/>
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://motivlabs.gitbook.io/impulse-user-manual/content-manipulators-motators/regex.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
