What is Regex?

Follow

Overview


Discounts on the Spark Pay system are capable of using Regular Expressions (REGEX). This is a powerful matching language that allows the creation of unique discounts.

 

Using RegEx in Discounts


You will find the regex feature in places throughout Spark Pay system. In this example below, it is being used in a discount method only associated if specific items are in the cart.

4-12-2014_4-45-12_PM.png

 

For example, someone wants to set up a discount to off $1.00 off all items containing 4433M and 200 in the item number. They would the regex for the unique items to be:

4433M-*[2][0]{2}$

RegEx Characters


 

MetacharacterDescription
. Matches any single character (many applications exclude newlines, and exactly which characters are considered newlines is flavor-, character-encoding-, and platform-specific, but it is safe to assume that the line feed character is included). Within POSIX bracket expressions, the dot character matches a literal dot. For example, a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c".
[ ] A bracket expression. Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z]matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z].

The - character is treated as a literal character if it is the last or the first (after the ^, if present) character within the brackets: [abc-][-abc]. Note that backslash escapes are not allowed. The ] character can be included in a bracket expression if it is the first (after the ^) character: []abc].

[^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter from "a" to "z". Likewise, literal characters and ranges can be mixed.
^ Matches the starting position within the string. In line-based tools, it matches the starting position of any line.
$ Matches the ending position of the string or the position just before a string-ending newline. In line-based tools, it matches the ending position of any line.
( ) Defines a marked subexpression. The string matched within the parentheses can be recalled later (see the next entry, \n). A marked subexpression is also called a block or capturing group. BRE mode requires \( \).
\n Matches what the nth marked subexpression matched, where n is a digit from 1 to 9. This construct is vaguely defined in the POSIX.2 standard. Some tools allow referencing more than nine capturing groups.
* Matches the preceding element zero or more times. For example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on. (ab)* matches "", "ab", "abab", "ababab", and so on.
^{m,n}$ Matches the preceding element at least m and not more than n times. For example, ^a{3,5}$ matches only "aaa", "aaaa", and "aaaaa". This is not found in a few older instances of regular expressions. BRE mode requires \{m,n\}.

 

RegEx Examples


Regular expressions can get incredibly complex if you let them. If you don't understand them, keep it simple and you'll be ok. We'll provide some example match codes here that should cover most situations:

Some rule types allow for a target quantity. This is a threashold required to meet the rule. (ex: item in cart rule can require a particular quantity to be present). Target quantity type options allow for the rule to apply only to multiples of the matched quantity or all matched quantity over a minimum target.

Product custom field in cart summed means it will conver the value of the custom field to an integer, sum it, and multiply it by the quantity of the cart item, then use the target quantity to evaluate.

 

You can use the following example to help with the format that the regular expression needs to be.

Match Code

Result

widgeta
matches just "widgeta"
widgetb
matches just "widgetb"
(widgeta|widgetb|widgetc)
matches "widgeta", "widgetb", or "widgetc"
widget(a|b|c)
matches "widgeta", "widgetb", or "widgetc"
widget.
matches "widgeta" or "widgetb", but NOT "widgetax" or "widget"
w.dget
matches "widget" or "wadget" ... any single char where the dot is
.*widget.*
matches any item number with widget anywhere inside, INCLUDING "widget" by itself
widget[0-9]
matches "widget0", "widget1", ..., "widget9", but NOT "widgeta"
NOTE: Regex strings are case-insensitive.
Have more questions? Submit a request

Comments