Regex dialect
addedMatches, removedMatches and droppedMatches patterns and
regex checks share one dialect of regular expressions, so that every
tool finds the same matches, whatever engine it uses. Its syntax is a
subset of ECMAScript's, limited to what RE2-style engines support, and
this section fixes its meaning as well as its syntax.
A pattern is matched against the text of one added or removed line at a time (see Conditions), which holds no line break. The match is an unanchored search: the line matches when the pattern matches starting at some position in its text. Characters are Unicode code points, and matching is case-sensitive. Only whether a line matches counts, never which part of it.
| Syntax | Matches |
|---|---|
A character other than \, ^, $, ., |, ?, *, +, (, ), [, ], {, } | itself |
. | any one character |
\ and an ASCII punctuation character (a printable ASCII character that is not a letter, a digit or a space) | that character: \. matches ., \{ matches { |
\t, \n, \v, \f, \r | a tab, line feed, vertical tab, form feed, carriage return |
\x and two hex digits (0-9, a-f, A-F) | the character with that code, from U+0000 to U+00FF |
\d, \D | an ASCII digit 0 to 9; any other character |
\w, \W | an ASCII letter, an ASCII digit or _; any other character |
\s, \S | a space, tab, line feed, vertical tab, form feed or carriage return; any other character |
[...], [^...] | one character in the set; one character not in it |
^, $ | the start of the text; the end of the text |
\b, \B | a word boundary; a position that is not one |
(x), (?:x) | x, as a group |
x|y | x or y |
x*, x+, x? | x zero or more times, one or more times, zero times or once |
x{n}, x{n,}, x{n,m} | x exactly n times, at least n times, from n to m times |
- A word boundary is a position with a
\wcharacter on one side, and on the other a character that is not\wor the start or end of the text.\wis ASCII, soéand٣are not word characters. - In
{n},{n,}and{n,m},nandmare decimal numbers with no leading zero (0itself is allowed), at most 1000, andnis not more thanm. A{}quantifier's bound is itsm, or itsnwhen it has nom, and is counted as 1 when that number is 0. The product of the bounds of a{}quantifier and of every{}quantifier on a group that holds it is at most 1000, so(a{10}){100}is allowed and(a{10}){101}is not. {always starts a quantifier, so a literal{is written\{. A literal}or]is written\}or\].- An alternative or a group MAY be empty:
a|,(|s),()and(?:)are all in the dialect. An empty alternative matches the empty string. - A quantifier MAY be followed by
?, which makes it lazy. Laziness changes which part of a line matches, so it changes no result. - A set holds one or more members up to its closing
], the first unescaped]after it. A member is a character other than\,[and]; an escape from the table that stands for one character, such as\],\-,\tor\x41; or one of the class escapes\d,\D,\w,\W,\sand\S. A^directly after[makes the set negated; a^anywhere else is a member. - A set's members are read from left to right. A member that stands for
one character, followed by an unescaped
-and then another such member, forms a rangex-ywith them: the characters fromxtoyby code point, wherexis not aftery. Any other unescaped-is a member only when it is the set's first or last. So[a\-z]holds three members,[--a]is the range from-toa, and[a-]holdsaand-. - There are no flags, inline or otherwise. Matching is always as above.
To match without regard to case, write the cases out:
[Uu][Ss][Dd].
Anything else is regex-outside-dialect. That includes:
- any group that starts with
(?other than(?:: lookahead and lookbehind ((?=,(?!,(?<=,(?<!), named groups ((?<code>or(?P<code>) and inline flags ((?i),(?i:); - backreferences (
\1to\9,\k<code>), and every other escape not in the table, such as\0,\A,\z,\p{L},\u0041,\cJand\é, and\bor\Binside a set ([\b]); - a
{that does not start a quantifier as described, as ina{,a{,2},a{01},a{1001}or(a{10}){101}, and an unescaped}or]outside a set; - a quantifier with nothing to repeat: at the start of the pattern, a
group or an alternative, after
^,$,\bor\B, or after another quantifier other than as its lazy?; - a group or set that is not closed, a
)that closes no group, an empty set ([],[^]), an unescaped[in a set, a range whose end comes before its start or that uses a class escape, and an unescaped-in a set that is neither part of a range nor its first or last member; - the empty pattern. (An empty alternative or group is allowed, as above; a pattern with no characters at all is not.)
A pattern MUST be one line. In addedMatches, removedMatches or
droppedMatches, a pattern holding a line feed or a carriage return is
regex-not-one-line, and Checks says when a regex check is
regex-not-one-line. A pattern is checked for regex-not-one-line
first, then for regex-outside-dialect. Both are entry-level errors, at
the opening tag line of the <when> or <check> that holds the
pattern.
A tool can run the dialect on an existing engine by translating it. For
example, an ECMAScript engine needs its u flag, so that it reads code
points, and its s flag, so that . matches every character. It also
needs \s and \S written out as sets, since its own are not ASCII, and
needs punctuation escapes that its u flag rejects, such as \',
written as the character. An RE2-style engine, such as Go's, needs \s
and \S written out too, since its \s leaves out the vertical tab.
Rust's and Python's standard engines make \d, \w, \s and \b
match Unicode by default and need their ASCII modes, and some engines,
such as Rust's, read \< and \> as word boundaries and &&, -- and
~~ in a set as set operations, so a translation writes those
characters so that the engine reads them literally.
Each of the first five patterns is outside the dialect: a lookahead, a
backreference, a named group, an inline flag, and a { that starts no
quantifier. Escaped, the brace is accepted, in addedMatches, in
removedMatches, in droppedMatches and in a check alike:
Example 76 (parse)
<rule id="currency-before-amount" category="functional_correctness">
<when>
addedMatches: ['USD(?=\d)']
</when>
A currency code is never written in front of an amount.
</rule>
<rule id="no-repeated-digits" category="functional_correctness">
<check>
(\d)\1
</check>
A price is never written with a placeholder of repeated digits.
</rule>
<rule id="currency-codes-from-settings" category="functional_correctness">
<when>
addedMatches: ['(?<code>USD|EUR)']
</when>
Display text never hard-codes a currency code.
</rule>
<rule id="currency-codes-any-case" category="functional_correctness">
<when>
addedMatches: ['(?i)usd']
</when>
Display text never hard-codes a currency code, in any case.
</rule>
<rule id="no-inline-styles" category="code_style">
<when>
addedMatches: ['style={']
</when>
Components never set inline styles.
</rule>
<rule id="no-inline-style-objects" category="code_style">
<when>
addedMatches: ['style=\{']
</when>
<check>
style=\{\{
</check>
Components never set inline styles; they use the design system's
classes.
</rule>
{
"entries": [
{
"kind": "rule", "id": "no-inline-style-objects", "file": ".lore/checkout.lore",
"category": "code_style", "severity": "advisory", "disabled": false,
"scopes": [{ "files": ["**"], "ignores": [] }],
"when": { "addedMatches": ["style=\\{"] },
"checks": [{ "lang": "regex", "body": "style=\\{\\{" }],
"meta": null,
"summary": "Components never set inline styles; they use the design system's classes.",
"prose": "Components never set inline styles; they use the design system's\nclasses.",
"src": null,
"sources": []
}
],
"errors": [
{ "name": "regex-outside-dialect", "file": ".lore/checkout.lore", "entry": "currency-before-amount", "line": 2 },
{ "name": "regex-outside-dialect", "file": ".lore/checkout.lore", "entry": "no-repeated-digits", "line": 9 },
{ "name": "regex-outside-dialect", "file": ".lore/checkout.lore", "entry": "currency-codes-from-settings", "line": 16 },
{ "name": "regex-outside-dialect", "file": ".lore/checkout.lore", "entry": "currency-codes-any-case", "line": 23 },
{ "name": "regex-outside-dialect", "file": ".lore/checkout.lore", "entry": "no-inline-styles", "line": 30 }
],
"warnings": []
}
٣ is ARABIC-INDIC DIGIT THREE (U+0663). It is neither an ASCII digit
nor an ASCII word character, so \d does not match it, and there is a
word boundary between it and U:
Example 77 (eligibility)
<rule id="price-digits-from-formatter" category="functional_correctness">
<when>
addedMatches: ['\d']
</when>
Prices in labels are formatted by the shop's number formatter, never
written as digits.
</rule>
<rule id="currency-code-from-settings" category="functional_correctness">
<when>
addedMatches: ['\bUSD\b']
</when>
Display text never spells out a currency code.
</rule>
M apps/web/src/checkout/labels.ts
+4 export const price = '٣USD'
{
"eligible": ["currency-code-from-settings"],
"hits": [
{ "rule": "currency-code-from-settings", "file": "apps/web/src/checkout/labels.ts", "line": 4, "text": "export const price = '٣USD'" }
],
"excluded": [],
"warnings": []
}
Line 4 holds a no-break space (U+00A0) between Total and USD. \s
is ASCII, so it does not match that space. An added line's text holds
no line break, so $ matches right after the ' that ends line 5:
Example 78 (eligibility)
<rule id="total-label-translated" category="functional_correctness">
<when>
addedMatches: ['Total\sUSD']
</when>
The total's label comes from the shop's translated strings.
</rule>
<rule id="fallback-currency-from-settings" category="functional_correctness">
<when>
addedMatches: ["USD'$"]
</when>
A fallback currency comes from the shop's settings, never from a literal.
</rule>
M apps/web/src/checkout/labels.ts
+4 export const heading = 'Total USD:'
+5 const fallback = 'USD'
{
"eligible": ["fallback-currency-from-settings"],
"hits": [
{ "rule": "fallback-currency-from-settings", "file": "apps/web/src/checkout/labels.ts", "line": 5, "text": "const fallback = 'USD'" }
],
"excluded": [],
"warnings": []
}
An empty alternative matches the empty string, so (|export ) matches
whether or not the line starts with export . The empty groups ()
and (?:) match the empty string too:
Example 79 (eligibility)
<rule id="tax-rate-from-settings" category="functional_correctness">
<when>
addedMatches: ['^(|export )const(?:) TAX_RATE()']
</when>
The tax rate comes from the shop's settings, never from a constant.
</rule>
M apps/web/src/checkout/tax.ts
+1 const TAX_RATE = 0.2
{
"eligible": ["tax-rate-from-settings"],
"hits": [
{ "rule": "tax-rate-from-settings", "file": "apps/web/src/checkout/tax.ts", "line": 1, "text": "const TAX_RATE = 0.2" }
],
"excluded": [],
"warnings": []
}