Skip to content

Regular expressions ​

A regular expression, often shortened to Regex, describes a pattern in text. Use one when a literal search is too specific: for example, to find every product code with the same structure rather than searching for each number separately.

Key points

Use a regular expression when you need a text pattern, such as a family of product codes. Test matching and nonmatching examples first. The effect depends on whether the pattern is used in a filter, replacement, QA rule, tag rule, or segmentation rule.

Searchspeare uses pattern fields in filtering, find and replace, custom QA, custom tags, and segmentation. The same pattern can have very different consequences in those tools, so first decide whether you are finding text, replacing it, flagging a problem, or changing a boundary.

Try a text search before using Regex ​

If you need every occurrence of “warranty”, use a normal text search. Turn on Regex only when you need pattern behavior.

In Regex mode, some punctuation has a special meaning. Searching for 1.5 can match a character between 1 and 5, because a dot means any character. Searching for 1\.5 asks for the literal decimal point.

Enter a pattern without wrapping it in slashes. Set case sensitivity through the relevant tool's controls.

Learn a few useful building blocks ​

PatternMeaningExample match
\dOne digit7
\d+One or more digits125
\sA whitespace characterA space or other whitespace
[A-Z]One letter in the specified rangeP
[A-Z]{2}\d{4}Two uppercase letters followed by four digitsAB1234
colou?rThe preceding u is optionalcolor or colour
cat|dogEither alternativecat or dog
^Note“Note” at the beginning of the text being checkedA segment starting with that word
end$“end” at the end of the text being checkedA segment ending with that word
\.A literal full stop.

A quantifier changes how many times the preceding element can occur. + means one or more; * means zero or more; ? means optional; {4} means exactly four. Keep the scope narrow: a pattern such as .* can match much more than you intend.

Try a practical filter ​

Suppose you need to inspect codes such as AB1234 and CD5678.

  1. Open the source or target filter settings.
  2. Enable Regex and case sensitivity if the uppercase form matters.
  3. Enter [A-Z]{2}\d{4}.
  4. Inspect the displayed segments and highlighted matches.
  5. Add more constraints if unrelated text also matches.

This finds the shape of a code. It does not prove the code is valid or present in an external product list.

Use capture groups for replacement ​

Parentheses capture a part of the match. Numbered references such as $1 and $2 reuse those captured parts in the replacement field.

FindReplaceIntended result
(Part)\s+(\d+)$1 $2Keep the label and number while normalizing the gap
(\d{2})/(\d{2})/(\d{4})$3-$2-$1Rearrange a date from day/month/year into year-month-day

The date example only recognizes and rearranges the shape. It does not validate a real calendar date and is unsuitable when the input could be month/day/year. Test assumptions before changing a whole file.

Read the pattern editor's feedback ​

The editor highlights pattern elements and can report invalid syntax. It can also make normally invisible whitespace easier to see. A pattern that is syntactically valid can still describe the wrong text, so do not stop at “no error”.

Test both a positive example that should match and a negative example that should not. For replacement, inspect the resulting text. For QA, inspect which source/target combinations are flagged. For segmentation, inspect both split and keep-together cases.

Enable Regex in Find and Replace to use pattern matching and expose Open regex library.
Enable Regex in Find and Replace to use pattern matching and expose Open regex library.
A named pattern example in the library. This form saves a reusable pattern; test its behavior in the tool where you will use it.
A named pattern example in the library. This form saves a reusable pattern; test its behavior in the tool where you will use it.
The Regex library lists saved patterns. Choose New regex to add one.
The Regex library lists saved patterns. Choose New regex to add one.

Save patterns and ask for help ​

The Regex library lets you reuse saved patterns through the available library controls. Give a pattern a name that explains its purpose and assumptions.

The Create rule with Searchspeare helper is available inside the pattern-based QA and segmentation rule forms. Describe the behavior and supply examples, then inspect the proposed fields. Accept fills the form; saving and applying the rule are separate steps. See Create rules with Searchspeare for screenshots and complete procedures for both forms.

Troubleshoot unexpected matches ​

If a pattern matches too much, check dots, optional elements, and repeated groups. If it matches nothing, check case sensitivity, escaping, whitespace, and the side being searched.

Whole-word filtering and Regex are separate modes; Regex controls its own boundaries. A word boundary that is useful for simple Latin text may be unsuitable for another writing system. Test the actual text you are working on.

For ordinary wording changes, return to Find and replace. For reusable quality checks, read QA.