Matching strings in User Properties and Event Properties is simple when you need to find a specific term such as "pecans." Segmentation allows you to search for when a string contains "pecans" anywhere in the string.
Searching for one of multiple items on a list within a field takes multiple "OR" criteria, and finding certain patterns like a person's phone number can be impossible if using just the "String Contains" criteria.
Regular expressions, or RegEx, allow you to search for patterns like these and many others.
In this article
- RegEx considerations in Iterable
- RegEx essentials: special characters
-
RegEx examples for everyday use
- Finding items that have a specific title or phrase
- Searching for terms that have international spellings
- Finding items with either upper case or lower case
- Finding people who have birthdays in the first three months of the year
- Find all two-letter state abbreviations as used by the USPS
- Find phone numbers (including multiple formats)
- Comparing current day of the week to a value on the user profile
RegEx considerations in Iterable
Many online resources exist for tips on how to use RegEx. Most assume you will be using RegEx while coding. Since performing Segmentation in Iterable does not require you to know be a proficient coder, there are a few key tips to know before creating your first RegEx expressions in Iterable:
-
RegEx values are case-sensitive
A RegEx of
California
will match any string that exactly resembles "California" ("california" would not be a match). Square brackets (eg.[Cc]alifornia
) allow you to account for different capitalizations. -
By default, any RegEx value entered in Iterable will be pinned to the beginning and end of the string that you're searching for (no need to include "^" or "$").
If you are searching for (case-sensitive) "Iterable", this is how you might approach it:
-
Iterable
will return just "Iterable". Think of this as an "equals" or "exact match" search. -
Iterable.*
will return "Iterable" and "Iterable Growth Marketing Platform", but not "Check out Iterable". Think of this as a "starts with" search. -
.*Iterable
will return "Iterable" and "Check out Iterable", but not "Iterable Growth Marketing Platform". Think of this as an "ends with" search. -
.*Iterable.*
will return anything containing a capitalized "Iterable". Think of this as an "contains" search.
TIP
".*" is a wildcard that suggests the possibility of any number of characters existing wherever it is placed (including the possibility of zero characters).
-
-
Iterable does not accept modifiers at the end of a RegEx search.
While in some coding languages you can add a
/g
or/i
or/m
at the end of some RegEx searches, Iterable will not read that those as modifiers and will instead search for those specific characters.
RegEx essentials: special characters
Most characters can be used normally. However, special characters can help you quickly search for a side variety of patterns. Here are some of the essential special characters to know as you build regular expressions:
Period: .
Matches any single character.
For example, man.
matches "many" and "mane".
Asterisk: *
Matches the preceding character or group zero or more times.
For example, yay*
matches "yay" and "yayyyyyyyy" and "ya".
Plus sign: +
Matches the preceding character or group one or more times.
For example, loo+k
matches "look" and "looooooook" but not "lok".
Question mark: ?
Matches the preceding character or group exactly zero or one times.
For example:
-
gre?a?y
matches "grey" and "gray" and "greay" and "gry" and -
colou?r
matches "color" and "colour".
Parentheses: ()
Creates a group to which other regular expression operations can be applied.
For example, ab+
matches "ab" and "abbbbbbb", but (ab)+
matches "ab" and
"abab" and "ababab".
Square brackets: []
Defines a set of characters, to make it possible to search for one character out of multiple.
For example, [Bb]eluga+ [Ww]hale
matches "beluga whale" and
"Belugaaaaaa whale" and "Beluga Whale".
NOTES
A caret symbol (
^
) inverts a set, so that it represents all characters except those listed between the square brackets. For example,abc[^xyz]123
matches "abcA123" and "abc5123" but not "abcy123".Inside square brackets, a hyphen defines a range or characters. For example,
[0-9]
will match any single digit number and[A-Z]
will match any capital letter. Similarly,[A-Z]+
will match any string that contains only capital letters.
Curly brackets: {}
Numbers in curly brackets specify the number of times a certain character should appear in a string in order to be considered a match. Curly brackets can be used to specify an exact number, a minimum, or a minimum and a maximum.
For example:
-
aweso(me){2}
matches "awesomeme" -
awesome{2,}
matches "awesomeeeee" and "awesomee" but not "awesome" -
awesome{2,4}
matches "awesomee" and "awesomeee" and "awesomeeee"
Vertical Bar: |
OR operator. Search for a match to the regular expression on either side of the vertical bar.
For example:
-
cat|dog
will match either "cat" or "dog". - cat|dog|bird|turtle will match "cat", "dog", "bird", or "turtle".
NOTE
You do not need to surround a regular expression containing a vertical bar with parentheses.
Backslash: \
An "escape character" that prevents regular expression special characters from performing their special actions. With the backslash, you can search for the literal plus sign, or asterisk, or any of the other special symbol.
For example:
-
1\+1
matches "1+1" -
[dD]estination\?
matches "destination?" and "Destination?"
NOTE
To match a literal, you can put a backslash in front of itself.
For example, \\
matches "\".
RegEx examples for everyday use
Finding items that have a specific title or phrase
Sample data:
- To search for a string that has "camera" in it, you could use regular
expression
.*camera.*
. This is the same as doing a search for a string that contains "camera". - To search for a string that begins with "camera", use regular expression
camera.*
. The.*
at the end of the expression indicates that any number of characters can come after the word "camera" (including zero).
Searching for terms that have international spellings
Some words have different spellings depending on what part of the world you are. For clients with international users, searching flexibly is useful.
Sample data:
Use regular expression .*colou?r
to search for various spellings of the
word "color".
In Segmentation:
In Handlebars (in a template):
This regular expression matches both "red color" and "red colour".
Match results:
Finding items with either upper case or lower case
Sometimes items or other user profile fields may have both upper case and lower case spellings.
For example, the following food iems contain both "chips" and "Chips":
But using the RegEx [cC]hips.*
would find anything that starts with "chips"
or "Chips", regardless of case. The .*
to match instances when "chips"
or "Chips" is followed by more characters (for example, "chips and salsa").
Match results:
Finding people who have birthdays in the first three months of the year
This example combines a few different regular expression techniques.
Problems to account for when searching the data:
- A month can be abbreviated ("Jan") or not ("January")
- A month can be capitalized ("January") or not ("january")
Regular expression: [Jj]an(uary)?|[Ff]eb(ruary)?|[Mm]ar(ch)?
Breaking it down:
- The vertical bars mean any one of the three sections could be a match.
-
[Jj]
and[Ff]
and[Mm]
search for upper and lower-case letters -
(uary)?
makes "uary" optional, matching either "January" or "Jan". -
(ruary)?
makes "ruary" optional, matching either "February" or "Feb". -
(ch)?
makes "ch" optional, matching either "March" or "Mar".
Example search data:
Match results:
Find all two-letter state abbreviations as used by the USPS
The following regular expression captures the two-letter state abbreviations used by the USPS:
(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])
NOTE
Edit this regular expression as needed to select on some states and not others.
Find phone numbers (including multiple formats)
The following regular expression:
([0-9]( |-|\.)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-|\.)?([0-9]{3}( |-|\.)?[0-9]{4}|[a-zA-Z0-9]{7})
Captures any of the phone number formats found on this list:
Comparing current day of the week to a value on the user profile
The following snippet examines a TargetDay
field on a user profile,
checking to see if it is set to the same day of the week as today.