Page Summary
On this page we discuss regular expressions as they are used by some plugins to define how to match and/or parse incoming data.
Regex Intro
Regex is short for Regular Expression.  A regular expression is a string composed of special syntax that defines how to match or parse another string.  As a simple example, the + character in regular expressions matches one or more instances of the preceeding character or capture group.  So a regular expression a+ would match a, aa, aab, baa, and baac because each of those strings have a sequence of one or more a characters.  Notice that regular expressions generally do not assume the match must start at the beginning of the string, though as we’ll see we can specify that as well.
Plugins often allow regular expressions to be used in configuration. This is helpful when, for example, matching and parsing string data received from an instrument. We’ll break down much more about regular expressions below, and provide many practical examples which are commonly used in JADE applications.
Basic Regular Expression Syntax
The basics of regular expression syntax is covered in the table below. This is not a complete / comprehensive syntax breakdown, but covers the most common regex syntax and is probably all you’ll ever need in JADE and beyond.
| Syntax | Description of Behavior | Example | 
|---|---|---|
| ^ | matches the start of a string | Regex ^abcmatchesabc,abcd, andabc123but does NOT matchxabc,aabc, orbc | 
| $ | matches the end of a string | Regex xyz$matchesxyz,axyz, and123xyzbut does NOT matchxy,ayz, oryz | 
| . | matches any single character | Regex a.cmatchesabc,aac,a3c,aZc, andva4cbut does NOT matchac,abd, orabbc | 
| * | matches 0 or more of the preceeding character or capture group | Regex ab*cmatchesac,abc, andabbcbut does NOT matchab,bc, oraad | 
| + | matches one or more characters or instances of a capture group | Regex a+cmatchesabc,abbc, andabbbcbut does NOT matchac | 
| ? | optionally matches a preceeding single character or capture group | Regex ab?cmatchesabcandacbut does NOT matchaec | 
| {n} | matches n instances of the preceeding character or capture group | Regex ab{2}cmatchesabbcbut does NOT matchabcorabbbc | 
| {n,m} | matches between n and m instances of the preceeding character or capture group | Regex ab{2,3}cmatchesabbcandabbbcbut does NOT matchabcorabbbbc | 
| {n,} | matches between n or more instances of the preceeding character or capture group | Regex ab{2}cmatchesabbc,abbbc, andabbbbc(and so on) but does NOT matchabcorac | 
| [...] | matches any single character specified between the flat brackets [and] | Regex a[bc]dmatchesabdandacdbut does NOT matchaec.  To match a literal flat bracked use\[or\] | 
| [^...] | matches any character except those specified between the flat brackets [and] | Regex a[^bc]dmatchesaedbut does NOT matchabdoracd | 
| (...) | parentheses around a subexpression defines a capture group | Regex a(*)cused against stringabccapturesb, used against stringabbccapturesbb, and used against stringaccaptures an empty string | 
| (?:...) | parentheses around a subexpression beginning with ?:indicates not to capture the subexpression | Regex a(?:b)matchesabcbut will not captureb | 
| (?=...) | parentheses around a subexpression beginning with ?=performs positive lookahead without capture | Regex a(?=b)matches theainabbut will not match theainac | 
| (?!...) | parentheses around a subexpression beginning with ?!performs negative lookahead without capture | Regex a(?!b)matches theainacbut will not match theainab | 
| (?<=...) | parentheses around a subexpression beginning with ?<=performs positive lookbehind without capture | Regex (?<=b)cmatches thecinbcbut will not match thecinac | 
| (?<!...) | parentheses around a subexpression beginning with ?<!performs negative lookbehind without capture | Regex (?<!b)cmatches thecinacbut will not match thecinbc | 
| (?&name) | matches a regular expression subroutine which has been defined with the specified name | JADE provides a common subroutine named numberwhich will match any number (integer, floating point, scientific notation).  To use this named regex, use the syntax:(?&number)For example, regex(?&number)matches10,10.5,10.5E2,10.5E+2, and10.5E-2.  Notably, it will not match a leading+character in front of a number. | 
| \c | matches a control character | Regex \cPmatches the character sequence entered when one typesCtrl + P | 
| \s | matches a whitespace character, including vertical whitespace characters | Regex a\sbmatchesa bbut does not match aXb | 
| \S | matches a non-whitespace character | Regex a\SbmatchesaXbbut does not match a b | 
| \d | matches a digit character, i.e. one of 0,1,2,3,4,5,6,7,8,9 | Regex a\dbmatchesa5bbut does not match aXb | 
| \D | matches a non-digit character, i.e. anything but 0,1,2,3,4,5,6,7,8,9 | Regex a\SbmatchesaXbbut does not match a b | 
| \w | matches a word character, which is essentially equivalent to [a-zA-Z0-9_]or upper and lower case letters, numbers, and underscores | Regex a\wbmatchesaXbbut does not match a b | 
| \W | matches a non-word character, which is essentially equivalent to [^a-zA-Z0-9_]or anything except upper and lower case letters, numbers, and underscores | Regex a\Wbmatchesa bbut does not match aXb | 
| \v | matches a vertical whitespace character; this is essentially equivent to the regex: [\r\n] | Regex a\vbmatchesabut does not match aXb | 
| \V | matches a non-vertical whitespace character; this is essentially equivent to the regex: [^\r\n] | Regex a\VbmatchesaXbbut does not match a | 
| \n | matches a line feed character | Regex a\nbmatchesa | 
| \r | matches a line feed character | Regex a\rbmatchesa | 
| \t | matches a tab character | Regex a\tbmatchesa b | 
| \f | matches a form feed character | Regex a\fbmatchesafollowed by aform feedcharacter, followed byb | 
| \0 | matches a null character | Regex a\0bmatches a string beginning witha, followed by a null character, followed byb | 
| \YYY | matches octal character YYY | Regex \x112matchesJ | 
| \xYY | matches hexadecimal character YY | Regex \x4AmatchesJ | 
Regex Examples
Below are several examples which show how to combine the syntax described above in meaningful ways.
| Example Regex | Description | Sample Matching Strings | 
|---|---|---|
| ^((?&number)),((?&number)),((?&number))\s*$ | matches a string which starts with 3 comma-delimited numbers and ends with 0 or more whitespace characters | 2,-30,52.0,-30.0,5.02,-3E1,5.0 | 
| ^((?&number))\s*,\s*((?&number))\s*,\s*((?&number))\s*$ | matches a string which starts with 3 comma-delimited numbers, with 0 or more whitespace characters around the commas, and ends with 0 or more whitespace characters | 2, -30, 5,2.0 , -30.0 , 5.0,2 ,-3E1 ,5.0 | 
Further Exploration
There’s more to the regex story and several great online resources which dive deeper.