- Phone numbers (
(123) 456-7890). - Email addresses (
user@example.com). - Product codes (
ABC-123-XYZ).
Recommendations
- Regex slots require careful crafting to avoid overly broad or restrictive patterns.
- Complex regular expressions can be error-prone. Test them thoroughly to avoid missing matches or capturing incorrect data.
- Ensure unique slot names to avoid conflicts with lexicon slots and system slots.
Work with regex slots
To start working with regex slots, you can use one of the following options:- Apply to an entire flow
- Apply after a specific point in a flow
To define a regex pattern that runs on all user inputs in the flow, follow these steps:
- Go to Advanced > NLU > Slot Fillers.
- In the upper-left corner, click + New Slot Filler.
- In the New Slot Filler sidebar, fill in the following fields:
- Name. Enter a unique name for the slot filler. For example,
phone_number. - Type. Select Regular Expression.
- Regular Expression. Enter a regex pattern in the format
/pattern/flags, for example,/\d{3}-\d{3}-\d{4}/i. Flags are optional. The slot filler stores only the first match, so thegflag has no effect. - Store result in Context. In the Advanced section, activate this toggle.
- Context Key. Enter a key to store the matched data in the Context object. For example,
phone_number.
- Name. Enter a unique name for the slot filler. For example,
Example
These examples show how a regex slot filler stores a match in the Context object. In both examples, Store result in Context is activated.Single match
Single match
User input:
Regex pattern:
Context Key:
Result:
My phone number is 555-123-4567Regex pattern:
/\d{3}-\d{3}-\d{4}/Context Key:
phone_numberResult:
context.phone_number is filled with 555-123-4567. The agent responds with Thanks for providing your phone number: {{context.phone_number}}!Multiple matches
Multiple matches
When the user input contains multiple values that match the pattern, only the first match is stored.User input:
Regex pattern:
Context Key:
Result:
The booking codes are 123456 and 789123Regex pattern:
/\b\d{6}\b/Context Key:
booking_codesResult:
context.booking_codes is filled with 123456.Use cases
- Extracting a booking code:
/^[A-Z]{3}-\d{4}$/to captureXYZ-1234. - Capturing email addresses with the
@companydomain:/^[a-zA-Z0-9._%+-]+@company\.com$/. - Identifying dates in specific formats:
/^\d{2}\/\d{2}\/\d{4}$/for04/22/2025.