Dodo doc > Syntax > Literal Values > Pattern Literals
A regular expression is used to match a text string against a pattern. They are enclosed in forward slashes. The backslash is used to escape special characters in the regular expression or to start a command. The special characters are:
\ escape character
( open grouping expression
) close grouping expression
[ open character alternatives
] close character alternatives
^ beginning of line
$ end of line
| alternative
* repetition
+ non-empty repetition
? option
{ open repetition count
} close repetition count
. wildcard
/ end of regular expression
Examples:
/file[0-9]{3}\.txt/
/^(CA\$H)*$/
A pattern can be a single value, a tuple, a list or a map containing patterns. In the case of a map, only the values can be patterns. The expression matches if it is structured the same way as the pattern and has matching values.
The special pattern underscore "_" matches any value.
Examples:
prettyHouse(wallColour: green) (-1.0, 3.3, false) [["a", "b"], ["c"], [\t]] [firstName: /^F/, lastName: /^N[ae]/] _
Any expression can be matched to a variable name prefixed with "$". A new variable is created for the match. Variables that are not prefixed with "$" must match exactly except for the attributes specified inside brackets (optional). To match only some attributes use "..." inside the brackets.
In a list, the last item can be a variable pattern prefixed with "..." to denote any number of values. The associated variable (optional) is a list with all the remaining items.
Examples:
$person(firstName: $name) child(firstName: $name, ...) [x, ...$tail]
An extended pattern adds a question mark after the structured or variable pattern to specify a type that should be matched.
Examples:
$house? Mansion _(size: $fruitSize)? Fruit
An interval defines a start value, an end value or both. The values must be Ordered (for pattern matching). An interval can also be used in a loop and in an index, where the values must be Enumerable. The start and end values must be of the same type if both are defined.
The bounds are included in the interval.
Examples:
1...10
...100
-0.8...
If the type of the values is Arithmetic the interval start or end value can be replaced with a span size. The span size is added to the start value or deducted from the end value to find the other bound. However, unlike an explicit start or end value, the calculated bound is excluded from the interval.
Similarly, when used in a loop or an index it is possible to define the interval with a span size instead of specifying both a start and an end value.
The span size is written with a star before the value and with braces around. There should be no space between the opening brace and star.
Examples:
6...{*4} # matches the span of values 6, 7, 8 and 9
[for i in 6...{*4}] # ranges over 6, 7, 8 and 9