I am using MySQL 8.0 with HeatWave and need to find the last occurrence of a pattern in my text. For instance, the pattern is like AuditKey: XXXXX-XXXX-XXXX-XXXX, and it can appear multiple times in a specific field.
SELECT field_name FROM my_table WHERE some_condition;
-- Result: text:1:status:ACTIVE:Example: AuditKey: ABCD-1234-5678-9XYZ|Data:1:Error: Issue: AuditKey: QRST-9876-5432-1ABC|
From the example given, my goal is to extract only the last AuditKey which in this case is QRST-9876-5432-1ABC. The AuditKey could be present 0, 1, or more times in the field.
In BigQuery, I could achieve this with:
ARRAY_REVERSE(REGEXP_EXTRACT_ALL(field_name, r'Audit[Key]*[=|: ]*([A-Za-z0-9]+-[A-Z0-9]+-[A-Z0-9]+-[A-Z0-9]+)'))[SAFE_OFFSET(0)]
However, MySQL does not have the REGEXP_EXTRACT_ALL function. I attempted to use REGEXP_SUBSTR(), but that only returns the first result. What is the proper way to retrieve the last occurrence of the AuditKey pattern in MySQL / HeatWave?