Parsing Emails with Regular Expressions

Last Updated -

This article reviews the basic and common uses of Regular Expressions and has some examples to demonstrate its power. Please continue reading the below to learn more. 

In Desk, we can use email rules to check for keywords in the Subject or the Body of the email. Then we can perform appropriate actions as needed, such as sending a specific Auto Acknowledgement email to the customer and set the case assigned to a specific group or set the case priority. 

Sometimes however, this isn't powerful enough to find certain pieces of information in an email that may be using a specific format such as a phone number with dashes or a unique pattern in an account number. This is were the use of Regular Expressions comes in handy. 
 

Introduction - A little about Regular Expressions

A regular expression (a.k.a, 'regex' or 'regexp') allows you to search a body of text for a pattern or specific sequence of characters. Then you can perform actions on these strings of text or patterns. This is very much how a word processing program might use a "find and replace" feature. It can be used for searching, replacing or validating and reformatting text. 
 

Check out the below article to see an example of a regular expression being used in an Inbound Interaction email rule.  Using Regular Expressions in Rules


Quick Reference - Examples of Common Regular Expressions

This OR That:
this|or|that - this code looks for "this" OR "or" OR "that".

  • Use this when you need to check for lots of keywords in a case. You can stick them all in one Condition, so you don't run out of space for other Conditions.

Specific Word Boundary:
\bKen\b - this code looks for "Ken" but only when it isn't part of another word. It finds Ken, but not Spoken.

  • Use this when you need to ensure you only find that keyword, and not that keyword contained within other words.

 

Specific Phrase:
(my phrase) - this code looks for the full "my phrase" in the brackets, but ignores the words "my" and "phrase" when used separately.

  • Use this when you need to ensure you only find that keyword, and not that keyword contained within other words.

 

Check if Field is blank/null:
^$ - this looks at the field to check if it is empty/null

  • Use this when you need to ensure a field hasn't been left empty

Parse Info from a Case and store it in Custom Field

Imagine you have regularly receive support tickets via a custom contact form or a third-party system which sends you a plain-text email containing information about the contact form which the email was submitted from, as well as the customer who submitted the form, e.g.,

Contact Form Info
Name: Email us!
Address: https://superawesomeplaceholderwebsitename.com/contact

Customer Info
Name: Han Solo
Career: Space Captain
Ship Name: Millenium Falcon

You could create Customer custom fields to hold the customer's "Career" and "Ship Name" information, a Case custom field to hold the contact form's information, and then create a rule under Admin > Cases > Rules > Inbound Interactions with the following rule actions:

  • Set Customer Name {{ email_in.body | scan: 'Name: (.*)', '1'}}
  • Set Customer Career {{ email_in.body | scan: 'Career: (.*)'}}
  • Set Customer Ship Name {{ email_in.body | scan: 'Ship Name: (.*)'}}
  • Set Case Form Address {{ email_in.body | scan: 'Address: (.*)'}}

Notice that in the first rule action we add ', 1' to the Liquid statement, indicating that we want the second match for the expression. If you need to specify which match should be used, it is important to remember that the matches are 'zero-indexed', so the first match is "0", the second match is "1", the third match is "2", and so on.

Here are some more examples for a use case using Regular Expressions. If you regularly receive emails which always include a line somewhere in the body with a unique ID composed of a capital letter followed by two digits and a hyphen, then ending in anywhere from zero to four letters of either case and one last digit, you could use a regular expression to scan the email for that ID. Consider the following four examples for such an ID:

  1. A42-b6
  2. Z99-YhXf4
  3. L00-aaaa8
  4. X01-1

Below are several ways to write the regular expression which would match any of those four IDs, e.g.,

Here's one way (Example in action)

[A-Z][0-9][0-9]-[A-z]{,4}[0-9]

And another way (Example in action)

[A-Z]\d{2}-[A-z]{,4}\d

How can I use this to pull info from Inbound Cases and store it in a Custom Field?

We added a filter to the Liquid markup available in Desk to support regular expressions (if you're not yet savvy on how filters work in Liquid, pop over to Liquid's documentation on GitHub real quick).

The filter is called scan, and it is used in an Inbound Interaction rule to parse text in email subjects and bodies. If you create a Case Custom Field called "Unique ID" and want to populate that field with, e.g., the example IDs above, you would create an Inbound Interaction rule (be sure to select "email" when creating the rule!), give it a rule action of "Set Case Unique Id", and use the scan filter to grab that ID from the email body by entering the following in the "Set Case Unique Id" field:

{{ email_in.body | scan: '[A-Z]\d{2}-[A-z]{,4}\d' }}

Here's a link to an awesome site where you can test and play with regular expressions: Rubular.com