# Replace in String

The Replace in string step performs a simple search and replace. This step supports regular expressions and group references. Group references are shown in the **Replace with** column as `$n`, where `n` is the number of the group.

### Step name

Enter the following information in the transformation step field:

* **Step name**: Specify the unique name of the step on the canvas. The default is Replace in string. You can customize the name or leave it as the default.

### Fields string table

![Fields string table in Replace in string](https://773338310-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYwnJ6Fexn4LZwKRHghPK%2Fuploads%2Fgit-blob-ef584c68e35c309e27b627cc6018be7ac3addcdc%2FPDI_TransStep_Table_Replace-In-String.png?alt=media)

Use the following table to specify the replacement string and related options.

| Column                 | Description                                                                                                              |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| **In stream field**    | Specify the field to replace from the PDI input stream. Click **Get Fields** to add all fields from the input stream(s). |
| **Out stream field**   | Specify the new field name to output to the PDI stream.                                                                  |
| **Use RegEx**          | Select **Y** or **N** to indicate whether to use a regular expression to search for values.                              |
| **Search**             | Specify the string to search for.                                                                                        |
| **Replace with**       | Specify the string to replace the matched value.                                                                         |
| **Set empty string?**  | Select **Y** or **N** to indicate whether to replace null values with empty strings.                                     |
| **Replace with field** | Specify a field value to use as the replacement value.                                                                   |
| **Whole word**         | Select **Y** or **N** to indicate whether to replace the entire word of the matched value.                               |
| **Case sensitive**     | Select **Y** or **N** to indicate whether the search is case sensitive.                                                  |
| **Is Unicode**         | Select **Y** or **N** to indicate whether the search value is a sequence of Unicode characters.                          |

You can click **Get Fields** to add all fields from the PDI input stream.

### Example: Use regular expression group references

When using regex (regular expression) group references, matching patterns are represented by a sequence of integer values with a dollar sign prefix. For example, `$1` for the first group match, `$2` for the second group match, and so forth.

To replace a portion of the incoming value contained in **In stream field**, use a group value token to represent the portion of the string that the group matched.

For example, suppose the string value of **In stream field** is `Homer Simpson`.

To switch the first and last name:

1. Set **Use RegEx** to `Y`.
2. Set **Search** to `/([a-zA-Z]*) ([a-zA-Z]*)/`.

   Each pattern inside parentheses represents a group, so `$1` = `Homer` and `$2` = `Simpson`.

   ![Regular expression group example](https://773338310-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYwnJ6Fexn4LZwKRHghPK%2Fuploads%2Fgit-blob-4f4a09b8aefca7d7774c810b588ccdcee087e65b%2FPDI_TransStep_Table_Replace-In-String_Example.png?alt=media)
3. Set **Replace with** to `$2 $1`.

   This places `Simpson` first, followed by `Homer`.

For more information about regular expressions, see <https://www.regular-expressions.info/brackets.html>.

### Metadata injection support

All fields of this step support metadata injection. You can use this step with [ETL metadata injection](https://docs.pentaho.com/pdia-data-integration/pdi-transformation-steps-reference-overview/etl-metadata-injection) to pass metadata to your transformation at runtime.

### See also

* [Regex Evaluation](https://docs.pentaho.com/pdia-data-integration/pdi-transformation-steps-reference-overview/regex-evaluation)
