Regular Expression

From ContactsLaw Documentation
Revision as of 13:41, 19 August 2025 by Bradley Smith (talk | contribs)

A regular expression is a method for representing a search pattern. Compared to simple terms or phrases (which must appear literally within the text), regular expressions provide much greater power and flexibility.

Broadly speaking, they provide the following features:

  • Alternation (multiple choices)
  • Repetition (multiple occurrences)
  • Generalisation (character groups or classes)
  • Capturing and reusing text

The syntax and authoring of regular expressions is beyond the scope of this documentation. There are numerous resources available online.

Use in Expressions

When used inside expressions in ContactsLaw, regular expressions are denoted using a forward-slash (/) at the beginning and end of the search pattern, e.g. /dog|cat|pig/.

Additional characters may appear after the pattern (e.g. /red|blue/i) and are used to specify options that affect how the search is performed:

  • i - Ignore character case.
  • m - ^ and $ anchors match lines rather than the entire body of text.
  • s - . matches any character including line-breaks (which would otherwise be excluded).
  • x - Ignore white-space characters in the search pattern (useful for improving readability).

Text that is captured by a regular expression can be reused in "find and replace" operations. You refer to previously-captured text with $n, where n is the number of the capture group; e.g. $1 for the first group.

External Links