Recent searches


No recent searches

REGEXP_REPLACE every instance



Posted Mar 21, 2024

Hi. The Explore function reference documentation doesn't mention it, and the example doesn't cover it, but it seems REGEXP_REPLACE function in Zendesk Explore will only replace the very first occurrence of a match

Use cases

I want to use it to clear some numbers from a string but the numbers can occur multiple times (not determined how many).

This is to normalize some error messages so we can count which messages are most common (and need to remove the parts of errors that are unique references so we can aggregate the general messages).

Another use is sometimes we use fields where we want to redact som PII data from showing.

Example

Trying to replace numbers (simplified, but here we have both PII data and we want to remove unique references to errors can be grouped), and the messages themselves can vary a lot:

StatusCode was: 400 {"code":"400","message":"already in 123456","transactionId":"xudcegdjeysbjjeesehd"}}
StatusCode was: 400 {"code":"400","message":"already in 654321","transactionId":"dddcejdhekwbxxyejasr"}}

Expected

This would replace all occurrences of a pattern (i.e. all numbers in my example above):

REGEXP_REPLACE([OrderErrorMessage], "\d+", "█")

resulting in:

StatusCode was: █ {"code":"█","message":"already in █","transactionId":"█"}}
StatusCode was: █ {"code":"█","message":"already in █","transactionId":"█"}}

Actual

Only the very first occurence of pattern is replaced:

StatusCode was: █ {"code":"400","message":"already in 123456","transactionId":"xudcegdjeysbjjeesehd"}}
StatusCode was: █ {"code":"400","message":"already in 654321","transactionId":"dddcejdhekwbxxyejasr"}}

 

Workaround

This is the only way I have been able to approach this. Nest a number of regex_replace functions.

Akward to type, probably not great for performance, and hard to maintain:

REGEXP_REPLACE(
  REGEXP_REPLACE(
    REGEXP_REPLACE(
    REGEXP_REPLACE([OrderErrorMessage], "\d+", "█"),
      "\d+", "█"  
     ),
     "\d+", "█"
   ),
   "\d+", "█"
)

I tried to put a "global" flag into the expression (a few different ways), but all my attempts resulted in the replace operation not working at all.

For example, I tried this:

REGEXP_REPLACE([OrderErrorMessage], "/\d+/g", "█")

Is it possible to have the regex replace all matches?

I expected the REGEX_REPLACE to already work this way, but as it doesn't, is there another way to replace all matches?


0

1

1 comment

Is this something that anyone have experience in, or maybe Zendesk can weigh in here?

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post