Universally Documentation

Step-by-step guides, multilingual SEO tips, and best practices to help you translate and scale your WordPress website.

Pattern matching

A pattern is one rule that covers a family of terms. Instead of listing five thousand product codes, you describe their shape once: STK{n:3} means the letters STK followed by exactly three digits, which covers STK110 and STK002 and every other code of that form.

Set Match to Pattern in the rule drawer and Term becomes a pattern instead of literal text. This page is the full reference for what you can write there.

The idea

A pattern is fixed text plus one or more slots. Fixed text must appear exactly. A slot stands for the part that varies.

STK{n:3}
^^^  ^^^^
|    a slot: exactly three digits
fixed text

Everything outside the braces is literal, and nothing else is special. R#{n} matches R#11223, and the # needs no escaping. Only { and } are reserved.

The live preview

As you type a pattern, the drawer shows three things: what the pattern means in plain language, three generated examples that match it, and a box to paste a real string and see whether it hits.

Use the box. It runs the same matcher the translator runs, so a string that matches there will match on your pages, and one that does not will not. It is faster than reasoning about the syntax.

Slots

Slot Matches
{n} digits
{a} letters
{w} letters or digits
{h} hex characters, 0 to 9 and a to f
{s} letters, digits, hyphens, and underscores

A bare slot means one or more characters.

Choosing between {w} and {s}

{w} stops at letters and digits. {s} also allows hyphens and underscores, which is what you need when the varying part is a single slug:

#{w}   matches #springsale        not #spring_sale or #spring-sale
#{s}   matches #spring_sale, #spring-sale, and #springsale

{w} is deliberately punctuation-free, because punctuation has a second job. Two open-ended slots need a separator that neither of them can match, and keeping - out of {w} is what makes SKU-{w}-{w} possible.

The same trade applies to {s}. Because {s} contains -, a pattern like {s}-{s} is refused, and you would use {w} between the hyphens instead.

Bounds

Add a length to pin a slot down:

Written Means
{n:3} exactly three digits
{n:2-4} two to four digits
{n} one or more digits

The largest length you can ask for is 40.

Bounds are worth the keystrokes. STK{n:3} matches STK110 and ignores STK1234; STK{n} matches both. If your codes are a fixed width, say so.

Numbered slots for translations

A pattern paired with a translation needs numbered slots on both sides, so the captured values land in the right places:

Term         Page {n1} of {n2}
Translation  Seite {n1} von {n2}

The numbers matter because languages reorder. This is equally valid and puts the second value first:

Term         Page {n1} of {n2}
Translation  {n2} sayfadan {n1}. sayfa

Both sides must carry the same set of numbers. Page {n1} of {n2} paired with Seite {n1} is refused when you save, because the second value would have nowhere to go.

Unnumbered slots are fine for a Keep as is rule, where nothing is substituted.

Where a pattern matches

A pattern matches a whole word, not part of one. More precisely: the text is split on whitespace, edge punctuation is trimmed from each piece, and the pattern must match a piece from start to end.

rule:  Keep as is, pattern "#{w}"

"#hashtag"                  match
"This is #hashtag here!"    match, the trailing ! is trimmed
"(#hashtag)"                match, the brackets are trimmed
"#hashtag, #other"          both match

"This is#hashtag here"      no match, the word does not start with #
"a#hashtag"                 no match, same reason
"##hashtag"                 no match, the second # is not a letter or digit
"This is # hashtag"         no match, the space splits it into two words

The punctuation trimmed from the edges is exactly . , ; : ! ? ( ) ' ". Hyphens, underscores, slashes, and hashes are not trimmed, because slots and fixed text legitimately contain them. That is why ref:STK110 does not match STK{n}: the colon sits inside the word rather than at its edge.

Spacing differences inside a multi-word pattern are tolerated. Order Number {n} matches text where those two words are separated by a line break or a doubled space.

Multi-word patterns

A pattern can span up to four words:

RTX {n:4}              matches "The RTX 4090 is fast"
{a:2}{n:2} {n}{a:2}    matches "Ship to AB12 3CD please", a UK postcode

What a pattern will not accept

Patterns are deliberately narrow. A rule that matches too much silently stops translating real sentences, which is harder to notice than a rule that never fires. These are refused when you save, each with a message naming the fix.

Not enough fixed text. {n} alone would match every number on the page, and a{w} would match every word starting with a. Add fixed text, like STK{n}.

One character is enough when an ordinary word cannot contain it, so #{w} and /{n} are accepted. Hyphens, apostrophes, periods, and underscores do not count, because words contain them: {w}-{w} would match “well-known”.

More than four slots, or more than four words.

An unknown slot. {num} is not a slot. The five in the table above are the whole set.

A length that does not make sense, such as {n:9-2}, or one above 40.

Two open-ended slots with nothing between them. {w}{w} has no defined split point. Nor does 0{n}0{n}, because the 0 between the slots is itself a digit that either slot could match. Put something between them that neither slot matches, like REF-{w}-{w}.

A repeated number, such as Page {n1} of {n1}.

Braces are reserved in Pattern mode. If your term genuinely contains { or }, use Exact term, where nothing is interpreted.

Examples

Pattern Matches Does not match
STK{n:3} STK110, STK002, fixed-width product codes STK1234, STK special offer
STK{n} STK1, STK110, STK1234 STK on its own
R#{n} R#11223, an internal reference R# pending review
#{h:6} #3faf21, a hex colour in design docs #zzz
#{s} #spring_sale, #spring-sale, campaign hashtags #spring sale, two words
SKU-{a:2}-{n} SKU-AB-4471 SKU-A-4471, one letter short
v{n}.{n}.{n} v2.10.4, a release version v2.10
Page {n1} of {n2} Page 12 of 34 Page one of two

Cost

A pattern that matches an entire string resolves without calling the translator, so it costs no words and stores no translation. A page of product codes covered by one STK{n:3} rule is the cheapest thing the glossary does.

A pattern found inside a longer sentence still sends that sentence to the translator, with the matched text passed along as an instruction. See How rules are applied.

Pattern rules are capped at 50 per workspace, inside the overall limit of 500, because each one costs more to evaluate on every page view than an exact term does.

Was this helpful?