ByteChef LogoByteChef
AutomationWorkflowsAIAI AgentGuardrails

Custom Regex

Your own named regular expressions, for identifiers that no built-in detector covers.

Custom Regex runs your own named regular expressions against the text. It runs in the preflight stage and needs no model. Use it for values specific to your business, such as internal ticket IDs, order numbers, account formats, or study identifiers. Attach it under Check for Violations to block text with a match, or under Sanitize Text to replace each match with [name].

Properties

PropertyTypeDefaultDescription
Validate InputBooleantrueCheck the user input before it is sent to the model.
Validate OutputBooleantrueCheck the model response before it is returned.
PatternsArray of objectsNoneThe named patterns. Required, with at least one entry.

Each entry in Patterns has:

FieldTypeRequiredDescription
NameStringYesThe placeholder [name] under Sanitize Text, and the violation name under Check for Violations.
RegexStringYesA Java regular expression, or a /pattern/flags literal.

Regex syntax

  • Plain Java regex, for example ORD-\d{4}. Matching is case-sensitive.
  • Literal with flags, for example /ssn-\d{4}/i. Supported flags:
FlagEffect
iCase-insensitive (Unicode-aware)
mMultiline: ^ and $ match at line breaks
sDot-all: . matches line breaks
uUnicode character classes
xComments: whitespace and # comments in the pattern are ignored
gAccepted and ignored. All matches are always found.

Any other flag is an error.

Results

  • Under Check for Violations (customRegexCheck): one violation. If one pattern matched, the violation is named after that pattern; if several matched, it is named customRegex. info.patternNames lists the patterns that matched, and matchCount is the number of matches.
  • Under Sanitize Text (customRegexSanitize): each match is replaced with [name]. The patterns are applied one after another in the order listed, and Custom Regex rewrites the text immediately, so sanitizers attached after it see the placeholders. Special characters in a name, such as $ or \, are inserted literally.

Examples

Block order IDs, case-insensitively:

{
  "name": "customRegex_1",
  "type": "customRegex/v1/customRegexCheck",
  "parameters": {
    "patterns": [
      { "name": "ORDER_ID", "regex": "/ORD-\\d{4,}/i" }
    ]
  }
}

Mask several identifiers:

{
  "name": "customRegex_1",
  "type": "customRegex/v1/customRegexSanitize",
  "parameters": {
    "patterns": [
      { "name": "internal-id", "regex": "MY-INTERNAL-\\d{4}" },
      { "name": "ticket", "regex": "TCK-\\d{6}" },
      { "name": "study-id", "regex": "STUDY-[A-Z]{2}-\\d{3}" }
    ]
  }
}

With this configuration, ref MY-INTERNAL-1234 for TCK-987654 becomes ref [internal-id] for [ticket].

Limits and errors

Each of the following is an execution failure, so the request is blocked (Check for Violations) or the text is withheld (Sanitize Text):

ConditionLimit
Patterns is empty, or an entry has an empty Name or RegexNone
The regex does not compile, or uses an unsupported flagNone
The regex is too long4,096 characters
The text to scan is too long1,048,576 characters
A match takes too long (for example, catastrophic backtracking)10,000,000 character reads per scan

When several patterns exceed the match limit, all of them are reported in one error.

How is this guide?

Last updated on

On this page