Regex in a Blue Prism Code Stage: match, extract and validate
Updated 25 September 2026 · Every C# sample here compiles with the compiler that ships with Blue Prism.
Pulling an invoice number out of an email, a policy number out of a PDF's text, or checking that a PAN is well formed: all of these are regular-expression jobs. Doing them with InStr and Mid on the canvas is fragile. In a Code Stage, .NET's Regex does it in one line.
Setup
Add System.Text.RegularExpressions to Namespace Imports in Code Options on the Initialise page. Regex lives in System.dll, which every object already references. Without the import you get The name 'Regex' does not exist in the current context (C#), or 'Regex' is not declared (VB).
Get the first match
Inputs: Input Text and Pattern (Text). Outputs: Found (Flag) and Value (Text).
Match m = Regex.Match(Input_Text ?? "", Pattern, RegexOptions.None, TimeSpan.FromSeconds(5));
Found = m.Success;
Value = m.Success ? m.Value : "";
With Pattern = INV-\d{6} and the text "Please pay INV-004211 and INV-004212 by Friday", Value is INV-004211.
Taking the pattern as an input makes this one action reusable for every extraction in the process. It also avoids escaping headaches. A pattern typed into a Blue Prism Text data item is used exactly as written, so \d is just \d. Only a pattern written inside C# code needs a verbatim string (@"\d+").
Get every match as a collection
Output: Matches (Collection).
DataTable table = new DataTable();
table.Columns.Add("Match", typeof(string));
foreach (Match m in Regex.Matches(Input_Text ?? "", Pattern, RegexOptions.None, TimeSpan.FromSeconds(5)))
table.Rows.Add(m.Value);
Matches = table;
For the same text this returns two rows: INV-004211 and INV-004212. Loop over the collection on the canvas, or pass it to your next action.
Pull out several fields at once with named groups
When one line holds several values, name each group and read them by name:
Match m = Regex.Match(Input_Text ?? "", @"Order (?<id>\d+) shipped on (?<date>\d{4}-\d{2}-\d{2})");
if (!m.Success)
throw new Exception("No order line found in the text.");
Order_Id = m.Groups["id"].Value;
Ship_Date = m.Groups["date"].Value;
From "Order 5531 shipped on 2026-09-21 to Pune" this gives Order Id = 5531 and Ship Date = 2026-09-21. Named groups keep working when you later add a group to the pattern. Numbered groups (Groups[1]) shift.
Throwing when there's no match turns bad input into a clear Blue Prism exception with a readable message, which is better than an empty value that fails three stages later.
Validate a format
Regex.IsMatch returns true or false, which suits a Flag output. Anchor the pattern with ^ and $ so the whole value must match, not just part of it:
PAN_Valid = Regex.IsMatch((PAN ?? "").Trim().ToUpperInvariant(), @"^[A-Z]{5}[0-9]{4}[A-Z]$");
IFSC_Valid = Regex.IsMatch((IFSC ?? "").Trim().ToUpperInvariant(), @"^[A-Z]{4}0[A-Z0-9]{6}$");
- PAN: five letters, four digits, one letter, for example
ABCDE1234F. - IFSC: four letters for the bank, a zero, then six letters or digits for the branch, for example
HDFC0001234.
These check the format only. A value can be well formed and still not exist.
Always set a timeout
A badly written pattern run against a large block of text can backtrack for minutes. On a robot, that looks like a hung process with nothing in the logs. The TimeSpan.FromSeconds(5) argument in the examples above makes .NET give up and throw RegexMatchTimeoutException, which Blue Prism reports as a normal exception you can recover from. Nested repeats like (a+)+ or (.*)* are the usual cause.
Useful options
RegexOptions.IgnoreCase: matchinv-004211as well asINV-004211.RegexOptions.Multiline:^and$match at every line break, not just at the start and end of the text. Useful for line-by-line email bodies.- Combine options with
|:RegexOptions.IgnoreCase | RegexOptions.Multiline.
Related
- Dates in Code Stages, for once you've extracted a date as text
- Code Stage compile errors
More Code Stage guides
- How to write a Code Stage in Blue Prism: inputs, outputs, namespaces and references
- Filter a Blue Prism collection without a loop (DataView and LINQ)
- Remove duplicate rows from a Blue Prism collection
- Parse JSON in a Blue Prism Code Stage (no third-party DLL)
- Blue Prism Code Stage compile errors and how to fix them
- Dates in Blue Prism Code Stages: parsing, formats and robot locale
- Code Stage vs Loop stage: making Blue Prism collection work faster