Skip to content

OCR Extract Pattern

Type: ocr-extract-pattern  ·  Package: OCR Activities v1.2.5  ·  Output: Dictionary<string, string>

Since: v1.2.0

Combines OCR + regular expressions in one step: it recognizes the text of an image and, on that text, applies a set of patterns (regex) to extract specific fields. It returns a dictionary { field: value }. It is the most direct way to get structured data out of a scanned document (invoice number, date, total).

Runs OCR on the image (once) and, on the resulting text, evaluates each pattern of the patterns dictionary:

  • It takes the first match of each pattern in the whole text.
  • If the pattern has a capture group (...), the value is what the first group captures; otherwise, the whole match.
  • If the pattern does not match (or is empty), the field gets empty text "".
  • Patterns are case-sensitive; to ignore case, start the pattern with (?i).
ParameterTypeDefaultDescription
pathstring—Path to the image file (.png, .jpg, .bmp, .tiff).
patternsDictionary—Dictionary fieldName → regex pattern. Use a capture group to extract only the value. Values can be expressions.
ParameterTypeDefaultDescription
languagestring"eng"Text language: Tesseract code (eng, spa, eng+spa) or regional tag (es-CO). See language.
preprocessstring"none"Prior cleanup: none · auto · deskew · binarize. See preprocessing.

Returns a Dictionary<string, string> with one key per pattern in patterns (always all of them) and its text value: the data found, or "" if there was no match. Check for empty fields before using them.

Extract the number, date and total of a scanned invoice:

OCR Extract Pattern
path = invoicePath
language = "eng"
preprocess = "auto"
patterns = {
"number": "INV-(\d+)",
"date": "Date:\s*(\d{4}-\d{2}-\d{2})",
"total": "Total:\s*\$?([\d.,]+)"
}
→ output: data
If condition = isEmpty(data["total"])
└─ then:
Throw message = "Could not read the invoice total"
Log message = "Invoice " + data["number"] + " — Total " + data["total"]
MessageCause and fix
'patterns' is required.patterns is missing.
'patterns' must be a Dictionary<string, string> mapping field names to regex patterns.patterns is not a { field: pattern } object (for example, a list or a text).
Invalid regex for field '…': …That field’s pattern has a syntax error (unclosed parenthesis, unescaped \…).
File not found.The path does not exist on the robot’s machine.
Tesseract language '…' not found at '…'. Available: [eng, spa].The language code is not installed.