Regular Expressions
What are regular expressions, and how and where to use them in your Optable DCN
Last updated
What are regular expressions, and how and where to use them in your Optable DCN
Last updated
Short for regular expression, a regex is a string of text that allows you to create patterns that help match, locate, and manage text.
Regular expression (regex) syntax differs slightly between programming languages and platforms. Optable uses the RE2 Syntax. You can use an online tool like this one to test your regex.
Optable allows you to use regex when you define query audiences.
Regex can save you a lot of time and help you perform actions that would be impossible to perform otherwise. Suppose a trait for which you have thousands of possible values, and you want to query it for the word shoe
. You will want variations such as shoe
, shoes
, Shoes
, Running shoes
, but not horseshoe
, shoelace
. Listing all possible variations and exceptions would be nearly impossible.
With regex, you can do this: (^| )(S|s)hoes?([ .,;:!?]|$)
Breaking the expression down into its components:
()
are grouping instructions/modifiers together
^
means begins with
|
means or
(^| )
means the expression must either start with the next instruction, or with a space, thus preventing words like goody two-shoes
from being considered a valid query result, because of the -
before the word shoes
.
(S|s)
allows the the lower and uppercase version of s
. Another way of achieving this is by using modifiers such as (?i)
at the beginning of the query.
?
means the previous character, or group of character if between ()
, is optional. In the expression (S|s)hoes?
, the following values are considered valid: Shoe
, Shoes
, shoe
, shoes
.
[]
representing a list or a class of characters. For instance, [0-9]
means any numeric value, [A-Z]
means uppercase letters.
$
means ends with
The current expression ([ .,;:!?]|$)
means that the variations must end with (S|s)hoes?
, or can be followed by an empty space or specific special characters (but not others not listed, such as -
)