Paid Rule Regular Expressions

Updated 

In the Rule Engine, the Message Conditions Text applies to the text content of messages and can be used to apply a Rule to messages that contain keywords or phrases. This Message condition supports RegEx, or Regular Expression, syntax. An example of RegEx Test Data is shown below.

Use Case
Conditions

What would work?

Explanation
Check if the text is one of the exact phrases

In the following three sentences if you want to check if the text is one of exact given phrases:

  1. I want to buy a TV

  2. I want to return my order

  3. I want to check my refund status

Then the following RegEx would work:

^I want to buy a TV$|^I want to return my order$|^I want to check my refund status$

in the RegEx denotes the sentence should start with “I want…”

$ in the RegEx denotes the sentence should end with “....TV”

| in the RegEx denotes OR, it means either sentence 1 or sentence 2 or sentence 3

This matches the exact phrases. If it has spaces before or after the sentence, it won’t match.

Check if the text contains one of few phrases (need not be an exact match)

If you want to match:

  1. I want to buy a TV

  2. I want to return my order

  3. I want to check my refund status

For example, in the text, you want to match “Hey, I want to buy a TV soon”.

The following RegEx would work -

(I want to buy a TV)|(I want to return my order)|(I want to check my refund status)

This matches the phrases in the text. Need not be exact match.

( ) contains text phrase

| denotes OR

So it’s either phrase-1 or phrase-2 or phrase-3

Limitations:
This will also match "Rick and LIBI want to buy a TV"

To ensure that the start and beginning of these phrases are not part of another word, you need to use:

\b((I want to buy a TV)|(I want to return my order)|(I want to check my refund status))\b

Here you have surrounded the phrases with the word boundary. (\b Word boundary can be a start, an end of a sentence, space , or a punctuation mark.)

Check if a text contains a 7 digit number

If you want to check if a text contains a 7 digit number.

The following RegEx would work:

[0-9]{7}

In this, digits between 0 to 9 are expected to repeat 7 times exactly.

However, this will match 7 digits even if they are part of a word. Example apple1234567. To find the number separately you need to surround by a word boundary

\b[0-9]{7}\b


Check if a text contains 6 digit number in a specific format

If the format is:

AA-XXX-X-XX

where A is an alphabet and X is a digit.

The following RegEx would work:

\b[A-Z]{2}-[0-9]{3}-[0-9]{1}-[0-9]{2}\b

\b denotes the word boundary so the expression should be a separate word and not part of a larger word.
[A-Z]{2} denotes that there should be two capital alphabets
- denotes dash
[0-9]{3} denotes 3 digits.

 

Check for an email

If you want to check for an email.

The following RegEx would work:

\b(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))\b

The entire regular expression ensures a particular format of alphanumeric expression to be matched.

Limitations:

Invalid email might also be captured by this, but this RegEx should work in most situations.

Check for phone number

If you want to check for a phone number. A phone number can be of several formats:

  • XXXXXXXXX

  • XXX XXX XXXX

  • XXX-XXX-XXXX

  • (XXX) XXX XXXX

  • (XXX)-XXX-XXX

The following RegEx would work:

\b\+?[0-9]{0,3}-?\s?(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}\b

The entire regular expression ensures a particular format of alphanumeric expression to be matched.

Check for emoji-only in messages

If you want to check if a message contains only emoji.

The following RegEx would work:

^(([\x{1F000}-\x{1F98F}]|[\x{2B00}-\x{2BFF}]|[\x{2702}-\x{27B0}]|[\x{2100}-\x{23FF}]|[\x{1F68}-\x{1F6C}]|[\x{1F30}-\x{1F70}]|[\x{2600}-\x{26ff}]|[\x{D83C}-\x{DBFF}\x{DC00}-\x{DFFF}]|([\x{20E3}]))){1,}$

The entire regular expression ensures a particular format of alphanumeric expression to be matched.

Check if a message contains any emoji

If you want to check if a message contains any emoji

The following RegEx would work:

([\x{1F000}-\x{1F98F}]|[\x{2B00}-\x{2BFF}]|[\x{2702}-\x{27B0}]|[\x{2100}-\x{23FF}]|[\x{1F68}-\x{1F6C}]|[\x{1F30}-\x{1F70}]|[\x{2600}-\x{26ff}]|[\x{D83C}-\x{DBFF}\x{DC00}-\x{DFFF}]|([\x{20E3}]))

The entire regular expression ensures a particular format of alphanumeric expression to be matched.

Check for specific emojis

If you want to check for specific emojis

You can check for specific emojis by adding emojis to the following RegEx:

Emoji_Heart_Eye.pngEmoji_thinking_face.pngEmoji_shy.jpgEmoji_Happy_teeth.png

Emoji_Heart_Eye.png|Emoji_thinking_face.png|Emoji_shy.jpg|Emoji_Happy_teeth.png)

If you want to add other emojis to the above, then separate by a pipe | and add them.

The entire regular expression ensures a particular format of alphanumeric expression to be matched.

Filter Test Data
Expected Result
Negative Flow
Test Result

efg.*

Filters messages that contains "efg(anything...)"

efanything,
fganything,
eganything

Pass

\bbrooke

Matches the brooke in "brooker" or "brooke535343" but not in "Abrooke" (because the "brooke" occurs inside a word and not immediately after a word boundary)

Abrooke or 123brooke

Pass

Colou?r

Filters messages "color" and "colour" (the 'u' is optional in this filter test data) 

colr or colur

Pass

a+

Matches ab and aaab

bbbba

Pass

ab.

Matches abc, ab_, abd (dot represents any single character)

absdkfd

Pass

  • Regex can be used in Rule Engine to match text in messages which are not possible with generic operators like "Starts with", "Ends with", "Exact match", "Contains", "Doesn't contain".
     

  • Rule Engine conditions based on “Starts with”, “Ends with”, “Exact match”, “Contains”, “Doesn’t contain” can be used to check limited text conditions. 
    For example, if you want to check if a message contains either a “Yes” or “No” or “Maybe”. You can do that using the exact match as following 
    Space_Rule_Engine_Condition.png

  • You can also combine all text conditions in one regex by using a regex ^Yes$|^No$|^Maybe$

There are some common operators in Regex which are used often.

Tokens
Description

[abc]

A single character of: a, b or c

[^abc]

A character except: a, b or c

[a-z]

A character in the range: a-z

[^a-z]

A character not in the range: a-z

[a-zA-Z]

A character in the range: a-z or A-Z

[a-zA-Z0-9]

Any character in range a-z or A-Z or 0-9

.

Any single character

\s

Any whitespace character

\S

Any non-whitespace character

\d

Any digit

\D

Any non-digit

\w

Any word character

\W

Any non-word character

(...)

Capture everything enclosed

(a|b)

Match either a or b

a?

Zero or one of a

a*

Zero or more of a

a+

One or more of a

a{3}

Exactly 3 of a

a{3,}

3 or more of a

a{3,6}

Between 3 and 6 of a

^

Start of string

$

End of string

\b

A word boundary

\B

Non-word boundary

RegEx is case sensitive. Upper or lower case matters. If you want to make case-insensitive RegEx, you have to surround your RegEx with (?i) and (?-i)

For example:

^I want to buy a TV$|^I want to return my order$|^I want to check my refund status$ (RegEx matches exactly “I want to buy a TV” and not “I WANT to buy a TV”)

If you want to make case-insensitive RegEx, you have to surround your RegEx with (?i) and (?-i)

(?i)^I want to buy a TV$|^I want to return my order$|^I want to check my refund status$(?-i)

Note

(?i) denotes that any RegEx after this will be case-insensitive till (?-i) is found in RegEx.

To make all RegEx case-insensitive, leave (?-i) in the RegEx.

For example, in the above RegEx, use (?i) expression only at the beginning of the sentence and not at the end.

(?i)^I want to buy a TV$|^I want to return my order$|^I want to check my refund status$

In Rule Engine, RegEx can be used in two primary ways.

  • To compare text in Rule Condition, and

  • To extract some text from message and store in the custom field in Rule Actions.

To compare text in Rule Condition

Text can be compared in rule engine condition by using conditions under Conditions Applies to "The properties of the message". Select Text and RegEx in the drop-down and then enter the expression.

Space_Rule_Engine_Properties of the message.png

Note: The RegEx matched is case-sensitive by default. You need to use (?i) to make it case insensitive.

To Extract Text from Message and Store in the Custom Field 

In Rule Engine, when you add Action, under Actions To "Change properties of Message" select Search pattern and take action to copy parts of text from message to a custom field based on RegEx match. For example, it can be used to copy phone number in the message to a custom field. It searches for given RegEx in a message and if found, copies the matched text to a given custom field. 

Space_Rule_Engine_Search_Pattern.png

Note: The RegEx used in the following action is case-insensitive by default.

  1. Click the New Tab icon  Screen Shot 2017-09-25 at 1.52.25 PM.png . Under the Sprinklr Marketing tab, click Manage Rules.

  2.  In the top right corner of the Rule Engine window, click Create New Rule. To learn more about how to create a new rule, see Create a Rule. 

  3. Select Customer as Rule Scope, and Inbound as Context.

  4. Click the Addition icon ,  and select Add Action.

  5. Under Actions To "Change Properties Of Message", select Search Pattern and Take Action from the dropdown list . Select Yes from the corresponding dropdown.
     Rules Search Pattern AndTake Action in rule Engine

  6. Text and Regex will auto-populate in their respective sections as per the action. Type the Input Text (in the form of Regex) you wish to copy and trigger via Rule Engine.

  7. Click Add Action within the same action "Change Properties of the Message" and select Add to Copiable entities from the dropdown list.Regex Added To Copiable Entities

  8. Click Save to confirm your rule.

Note

  • In the Middle Pane, if the regex matches any part of the existing text entities that include mention /url/hashtags etc then it will not be considered to show as a copiable entity as the above entities are given priority over copiable.

  • Also, overlapping copiable texts are not considered - if two rules apply two different regex that overlap - both cannot be considered, only one of them will work and the other overlapping one will be discarded.