Skip to main content

Working with Regex

Intro

Regular Expressions (shortened to Regex) can be used in various places in Cavalry including the Regex String Manipulator, Material/Style Behaviours like Apply Font Size and the JavaScript Layers.

Regex is a sequence of characters that specify a search pattern. A common use for regex is to match strings or parts of strings in order to do something with the result. For example, the expression a+ will return all instances of the letters a found within the input string. So an input string of Cavalry would return aa because it contains two as.

Online platforms like Regex101 are a great resource for learning and testing regular expressions.

Regex Flavours

JavaScript Layers use ECMAScript regex.

Text and Style Behaviours (Apply Typeface, Apply Font Size, etc.) use ICU regex, which supports inline flags for case-insensitive matching and other options.

Examples

Use with the Regex String Manipulator

The Regex String Manipulator will return only the result of the regular expression.

StringRegexDescriptionResult
Hello World.\sRemove SpacesHelloWorld.
As Capably as Cavalry.C.....y7 letter words that start with C and end with yCapablyCavalry
Is it Colour or Color?Colou?rColour or ColorColourColor
3 is the magic number.\dAny number3

Use with the Style/Material Behaviours

Style Behaviours (Apply Typeface, Apply Font Size and Material Behaviours like Apply Text Material) use the result of the regular expression to affect the font, appearance or character spacing.

StringLayerRegexDescriptionResult
How are you today?Apply Typeface\b(today)\bReturn the word 'today'.How are you today?
Animate in CavalryApply Text Material\b(Cavalry)\bReturn the word 'Cavalry'The word Cavalry will be colored with the Fill

Capture Groups

A capture group is a way to store and extract specific parts of the string being matched. Wrapping parts of a regular expression in parentheses creates groups which can then be filtered by entering their Ids into the Capture Group Indices attribute.

For example, given the string hello@scenegroup.co, the following regular expression can be used to separate the name, domain and top level domain - \b(\w+)@(\w+)\.(\w+)\b.

This will save capture groups with their indices set as follows:

IndexContents
1hello
2scenegroup
3co

The capture groups can then be used by entering their Ids into the Capture Group Indices attribute. For example, entering 2 to an Apply Typeface where a bold typeface is selected would result in - hello@scenegroup.co. Alternatively, setting Capture Group Indices of 1,3 would result in - hello@scenegroup.co.

Entering a value of 0 or leaving the Capture Group Indices attribute empty will return the full match.

Inline Flags

Inline flags modify how the regex engine matches patterns by placing them at the start of an expression. These flags are supported in all Style and Material Behaviours.

FlagNameDescriptionExample
(?i)Case InsensitiveMatches regardless of upper/lower case(?i)hello matches "Hello", "HELLO", "hello"
(?m)Multiline^ and $ match at line boundaries, not just start/end of string(?m)^Line matches "Line" at the start of any line
(?s)Dot All. matches newline characters as well as other characters(?s)a.b matches "a", newline, "b"
(?x)CommentsAllows whitespace and comments in the pattern for readabilityUseful for complex patterns

Flags can be combined: (?im) enables both case-insensitive and multiline modes.

Example

StringRegexResult
Typography is important. Good typography matters.(?i)(\btypography\b)Typography is important. Good typography matters.