Using regular expressions
Affinity supports the use of regular expressions to find document content that matches specified patterns. Regular expressions are widely used in word processing and DTP.
Regular expressions are used in several contexts in Affinity:
- To find (and optionally replace) document text that matches a pattern.
- To toggle the visibility of layers whose names match a pattern (via the States panel).
- To perform calculations in fields. See Field input and Expressions for field input for more information, including examples.
- To create Preflight warnings or errors when patterns are found in document text.
Regular expressions use special syntax to specify patterns. The examples below demonstrate common syntax you can use to write your own expressions.
A comprehensive guide to the full power of regular expressions is beyond the scope of Affinity Help. Learn more from online resources such as www.regular-expressions.info and regexone.com.
Case sensitivity
The States panel is case sensitive unless your expression explicitly states otherwise.
The Find and Replace panel is case sensitive when the Match Case option is enabled.
On each panel, case can be ignored for part or all of an expression:
- Ignore the case of a specific character by using a character class. For example, [aA] or [a-eA-E] to match the letter a or all letters from a to e.
- Ignore the case of all letters by starting an expression with (?i). For example, (?i)internet will match both Internet and internet.
Example regular expressions
Layer names containing either of two words followed by a specific word
In an Affinity document, some layers are named Dust removal or Blemish removal. To simplify layer management, we want to control their visibility simultaneously.
On the States panel, add this query: (?i)(Dust|Blemish) removal. This matches any layer name where Dust or Blemish is followed by removal, ignoring capitalization.
To toggle matching layers' visibility, click the query's Hide or Show button.
Layer names containing two words in any position and order
In a document, we've used the layer name offset fill to identify layers that were manually repositioned to create a handmade misprinted effect.
In some cases, the words may appear in reverse order.
On the States panel, add this query: (?i)(?=.*offset)(?=.*fill).*$. This matches any layer name containing both offset and fill, in any order or position, ignoring capitalization.
Layer names that follow a specific naming convention
In a CAD architectural design document, layer names identify building components using a two-letter prefix, followed by one or more groups of an underscore followed by some digits. For example, Ss-35_10_15.
On the States panel, you can control the visibility of layers belonging to a specific component category using an expression like [Ss][Ss](_[0-9]{1,})+. This matches names that begin with two S characters (regardless of letter case), followed by numeric groups.
To control the visibility of layers from all other component categories, modify the expression slightly: (^[Ss][Ss])(_[0-9]{1,})+. The caret (^) ensures the match occurs only at the start of the layer name.
Instances of a word in document text except where preceded by a specific word
We want to find all instances of a specific word in text, but exclude those that are directly preceded by another word.
With Regular Expression enabled in the Find and Replace panel's formatting options, you can use a 'negative lookbehind' to achieve this.
For example: (?<!proin )aliquam will match all occurrences of aliquam except those immediately preceded by proin and a space.
You can also exclude matches preceded by any one of several words. For example, to exclude instances of error when it's preceded by either the or an, use: (?<!(the|an) )error.
SEE ALSO:
How would you rate the help you received from this article?