> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portkey.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Replace Custom Regex Patterns

> Redact custom patterns with Regex in Portkey.

For more granular control over pattern redaction, you can create custom patterns using Portkey's **Regex Match** guardrail with redaction capabilities. This allows you to define specific regex patterns for sensitive information unique to your use case.

### Setting Up Custom Regex Patterns

![](https://mintcdn.com/portkey-docs/QKXLB-54q6gEhIad/images/guardrails/regex.png?fit=max\&auto=format\&n=QKXLB-54q6gEhIad\&q=85\&s=5557c90d78352ee9295bf1e18030fd92)

1. **Navigate to Guardrails**: Go to the `Guardrails` page and click `Create`

2. **Select Regex Match**: Choose the "Regex Replace" guardrail from the BASIC category

3. **Configure the Pattern**:
   * **Regex Rule**: Enter your regex pattern to match specific data (e.g., `\b\d{3}-\d{2}-\d{4}\b` for SSN patterns)
   * **Replacement Text**: Define what to replace matches with (e.g., `[REDACTED]`, `*****`, `[SSN_HIDDEN]`)

4. **Save the Guardrail**: Name your guardrail and save it to get the associated Guardrail ID

### Common Regex Patterns for Sensitive Data

| Pattern Type | Regex Pattern | Replacement Example |
| - | - | - |
| Credit Card | `\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b` | `[CREDIT_CARD]` |
| Social Security Number | `\b\d{3}-\d{2}-\d{4}\b` | `[SSN_REDACTED]` |
| Phone Numbers | `\b\d{3}[-.]\d{3}[-.]\d{4}\b` | `[PHONE_HIDDEN]` |
| Email Addresses | `\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b` | `[EMAIL_REDACTED]` |
| Custom Employee IDs | `EMP-\d{6}` | `[EMPLOYEE_ID]` |

### Adding to Your Config

Once you've created your custom regex pattern guardrail, add it to your Portkey config:

```json theme={"system"}
{
  "before_request_hooks": [
    {"id": "your-guardrail-id"}
  ],
  "after_request_hooks": [
    {"id": "your-guardrail-id"}
  ]
}
```

> [!NOTE]
>
> You can add the same guardrail to both `before_request_hooks` (input guardrails) and `after_request_hooks` (output guardrails) to scan and redact regex patterns in both user inputs and LLM responses.

### Example Implementation

> [tab] NodeJS
>
> ```js theme={"system"}
> const portkey = new Portkey({
>     apiKey: "PORTKEY_API_KEY",
>     config: "pc-***" // Config with custom regex pattern redaction
> });
>
> const response = await portkey.chat.completions.create({
>     model: "@your-model-slug",
>     messages: [
>         {
>             role: "user",
>             content: "My SSN is 123-45-6789 and credit card is 4532-1234-5678-9012"
>         }
>     ]
> });
> ```

> [tab] Python
>
> ```py theme={"system"}
> portkey = Portkey(
>     api_key="PORTKEY_API_KEY",
>     config="pc-***" # Config with custom regex pattern redaction
> )
>
> response = portkey.chat.completions.create(
>     model="@your-model-slug",
>     messages=[
>         {
>             "role": "user",
>             "content": "My SSN is 123-45-6789 and credit card is 4532-1234-5678-9012"
>         }
>     ]
> )
> ```

With the custom regex guardrail configured, the input would be automatically transformed to:

```
"My SSN is [SSN_REDACTED] and credit card is [CREDIT_CARD]"
```

***

> [card] [Portkey is now PRISMA AIRS AI Gateway. See it in action.](https://www.paloaltonetworks.in/ai-security/ai-gateway?utm_source=portkey\&utm_medium=referral\&utm_campaign=prisma_airs\&utm_content=docs_nav#contact)
>
> Contact Us


## Related topics

- [September](https://docs.portkey.ai/docs/changelog/2025/september.md)
- [PII Redaction](https://docs.portkey.ai/docs/product/guardrails/pii-redaction.md)
- [Enterprise Gateway](https://docs.portkey.ai/docs/changelog/enterprise.md)
- [Guardrails](https://docs.portkey.ai/docs/product/mcp-gateway/guardrails.md)
- [Custom hosts](https://docs.portkey.ai/docs/product/ai-gateway/custom-hosts.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.