Complex String Manipulation and Regex Extraction in Architect Expressions

Complex String Manipulation and Regex Extraction in Architect Expressions

Executive Summary & Architectural Context

In enterprise contact centers, data arriving into the IVR is rarely clean. A customer might enter their Account Number as 123-456-789, but your backend CRM API strictly requires 123456789. An ANI (Caller ID) might arrive from the carrier as +13125550199, but you need to extract just the Area Code 312 to route the call to a regional office.

If you cannot manipulate strings directly inside Genesys Cloud Architect, you are forced to build expensive, high-latency middleware to scrub data before processing it.

The engineering solution leverages the Architect Expression Language. Architect provides a robust suite of string manipulation functions, including substring extraction, padding, and fully compliant Regular Expressions (Regex). This masterclass details how to sanitize inputs, enforce data formatting, and extract dynamic patterns using Architect expressions.

Prerequisites, Roles & Licensing

  • Roles & Permissions: Architect > Flow > Edit
  • Platform Dependencies:
    • A solid understanding of standard Regular Expression (Regex) syntax.

The Implementation Deep-Dive

1. Basic String Scrubbing (Removing Formatting)

The most common requirement is stripping hyphens or spaces from user input.

  1. Create a Collect Input node to gather Task.RawAccount (e.g., 12-34 56).
  2. Add an Update Data action.
  3. Assign a new string variable: Task.CleanAccount.
  4. The Expression: Use the Replace function to strip out non-numeric characters.
    • Replace(Replace(Task.RawAccount, "-", ""), " ", "")
    • Logic: This nests two replace functions, first stripping hyphens, then stripping spaces.

2. Extracting the Area Code via Substring

To route based on area code, you must extract characters 2, 3, and 4 from an E.164 formatted string (+13125550199).

  1. Add an Update Data action.
  2. Assign variable: Task.AreaCode.
  3. The Expression: Substring(Call.Ani, 2, 3)
    • Logic: Start at index 2 (skipping the + at 0 and the 1 at 1), and extract exactly 3 characters.
    • Result: "312"

3. Advanced Regex Extraction

If a backend system returns a messy error string like ERROR_CODE: [X99-Y] - Validation Failed, and you need to extract just the X99-Y segment for routing logic, basic substring math will fail because the length is dynamic. You must use Regex.

  1. Add an Update Data action.
  2. Assign variable: Task.ExtractedCode.
  3. The Expression: Match(Task.RawErrorMessage, "\\[(.*?)\\]")
    • Note: You must double-escape the backslashes in Architect expressions when writing Regex literals.
    • Logic: This Regex looks for literal brackets [ and ], and extracts the dynamic string (.*?) between them.
  4. If no match is found, the Match function returns a blank string.

Validation, Edge Cases & Troubleshooting

Edge Case 1: Null or Empty Strings

If you run Substring on a string that is shorter than the requested index, Architect will throw a runtime error and dump the caller into the Error path.

  • Troubleshooting: Always validate string length before manipulating it.
  • Safe Expression:
    If(Length(Task.RawAccount) >= 5, Substring(Task.RawAccount, 0, 5), Task.RawAccount)
    
    This checks if the string is at least 5 characters long before attempting to substring it, preventing crashes.

Edge Case 2: Type Casting (String to Integer)

Data Actions strictly require correct JSON data types. If your CRM requires an Integer for the Account Number, but Architect collected it as a String via DTMF, the Data Action will return a 400 Bad Request.

  • Solution: Use the ToInt() cast function.
    • ToInt(Task.CleanAccount)
  • Warning: If Task.CleanAccount contains any letters, ToInt() will fail. Always use IsInt() in a Decision node to validate the string is purely numeric before casting.

Official References