Regular Expressions Reference
Regular Expressions, or regex, are powerful tools used to identify and manipulate patterns in text. Reflex Blue uses regex in various places such as when validating text-type Dynamic Fields or when defining file masks in the Import/Export Service.
Basic elements of Regular Expressions
Below we’ll take a look at the basic elements that form the foundation of regex patterns.
1. Literals
Literals match the exact characters they represent.
The regex pattern cat matches the string “cat” exactly.
2. Metacharacters
Metacharacters are symbols that represent special meanings in regex.
-
.(Dot): Matches any single character except a newline.The regex pattern
c.tmatches “cat”, “cut”, “cot”, etc. -
\d: Matches any digit (0-9).The regex pattern
\d\dmatches “12”, “34”, etc. -
\w: Matches any word character (alphanumeric or underscore).The regex pattern
\w\w\wmatches “abc”, “123”, etc. -
\s: Matches any whitespace character (spaces, tabs, etc.).The regex pattern
a\s\sbmatches “a b” (two spaces). -
\b: Matches a word boundary.The regex pattern
\bcat\bmatches “cat” in “a cat is here”, but not in “caterpillar”.
3. Quantifiers
Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found.
-
*: Matches 0 or more occurrences.The regex pattern
ca*tmatches “ct”, “cat”, “caaat”, etc. -
+: Matches 1 or more occurrences.The regex pattern
ca+tmatches “cat”, “caat”, but not “ct”. -
?: Matches 0 or 1 occurrence.The regex pattern
ca?tmatches “cat” or “ct”. -
{n}: Matches exactlynoccurrences.The regex pattern
a{3}matches “aaa”. -
{n,m}: Matches betweennandmoccurrences.The regex pattern
a{2,4}matches “aa”, “aaa”, or “aaaa”.
4. Character Classes
Character classes allow you to define a set of characters that can match at a particular position in the input.
-
[abc]: Matches any one of the characters a, b, or c.The regex pattern
[ch]atmatches “cat” and “hat”. -
[^abc]: Matches any character except a, b, or c.The regex pattern
[^ch]atmatches “bat”, “sat”, but not “cat” or “hat”. -
[a-z]: Matches any lowercase letter.The regex pattern
[a-z]matches any lowercase letter from “a” to “z”. -
[A-Z]: Matches any uppercase letter.The regex pattern
[A-Z]matches any uppercase letter from “A” to “Z”.
5. Anchors
Anchors assert a position in the input string, rather than matching a specific character.
-
^: Matches the start of a string.The regex pattern
^catmatches “cat” only if it is at the beginning of the string. -
$: Matches the end of a string.The regex pattern
cat$matches “cat” only if it is at the end of the string.
6. Groups and capturing
Groups allow you to group parts of a regex pattern together. Capturing groups also store the matched text for later use.
-
()(Parentheses): Groups multiple tokens together and creates a capturing group.The regex pattern
(abc)+matches “abc”, “abcabc”, etc. -
(?: )(Non-capturing group): Groups multiple tokens together without creating a capturing group.The regex pattern
(?:abc)+matches “abc”, “abcabc”, etc., but doesn’t capture the match.
7. Alternation
Alternation allows you to match one pattern or another.
-
|: Acts like a logical OR.The regex pattern
cat|dogmatches either “cat” or “dog”.
8. Escaping special characters
If you want to match a special character literally, you need to escape it with a backslash \.
The regex pattern \. matches a literal dot instead of any character.
Online resources for learning regex
If you’d like to deepen your understanding of regex, here are some excellent online resources: