Regular expressions

A regular expression, or pattern, describes the form of a text, for example "two capital letters, a hyphen and three digits". EFX uses regular expressions in two places:

  • the operator like, which tests whether a text matches a pattern;

  • the function replace-regex, which replaces the parts of a text that match a pattern.

The translator turns EFX expressions into other languages, such as XPath, and each of these languages has its own form of regular expressions. So that a pattern gives the same result in all of them, EFX accepts only a part of the usual syntax that all of them share. The first entries of this page explain how you write a pattern, and how EFX matches and checks it. The entries after them describe each part of the syntax that EFX accepts. The last entry lists the parts that EFX does not accept.

Regular expression

A regular expression is a pattern, written in a text literal, that like and replace-regex use.

Syntax
<text> like '<pattern>'
<text> not like '<pattern>'
replace-regex(<text>, '<pattern>', <replacement>)
<text>

the text to examine.

<pattern>

the pattern, made of the parts described on this page.

<replacement>

the text that replaces each match (see replace-regex).

You write a pattern as the content of a text literal, between single or double quotes. Inside the literal, \' and \" stand for a quote character, as in any text literal. Every other backslash stays, and is part of the pattern. For example, '[0-9]+\.[0-9]+' is the pattern [0-9]+\.[0-9]+, in which \. stands for a full stop.

A pattern can contain the following parts:

Part Entry

a, 1, -, … (characters that are not metacharacters)

Literal characters

.

Wildcard

*, +, ?, {n}, {n,}, {n,m}, and the same followed by ?

Quantifiers

[abc], [a-z], [^abc]

Character classes

\., \\, \(, …, \t, \n, \r, \', \"

Escape sequences

(…), |

Grouping and alternation

^, $

Anchors

\p{…}, \P{…}

Unicode properties

A metacharacter is a character with a special meaning in a pattern, such as . or *. The entry Literal characters lists them all.

Restrictions

  • With like, the pattern must be a text literal.

  • With replace-regex, the pattern can be any text, for example the value of a field or of a variable. However, the translator checks only a pattern written as a text literal (see Checking of patterns).

  • A pattern must use only the parts listed above (see Unsupported constructs).

Examples

BT-00-Text like '^[A-Z]{2}-[0-9]{3}$'

TRUE for a text such as AB-123.

BT-00-Text not like '[0-9]'

TRUE when the text contains no digit.

replace-regex(BT-00-Text, '[,;]', ' ')

Replaces each comma and each semicolon with a space.

EFX-1 did not check patterns. It passed parts such as \d, \s, \w and \b on to the target language, although they do not exist in the same form in every target language. EFX-2 reports them as errors. So you may have to rewrite a pattern written for EFX-1: \d becomes [0-9], and \s becomes [ \t\n\r] (see Unsupported constructs).

EFX-1 text literals also accepted only a few escape sequences, and did not turn \' and \" into quote characters (see Escape sequences). The function replace-regex is new in EFX-2.

Matching

Matching compares a text with a pattern.

A pattern matches a text when some part of the text has the form that the pattern describes. The operator like is TRUE when the pattern matches any part of the text, not necessarily the whole text: 'abc123' like '[0-9]+' is TRUE. To require the whole text to have the form of the pattern, start the pattern with ^ and end it with $ (see Anchors).

Before the match, like removes the spaces at the start and at the end of the text. It also replaces each run of spaces, tabs or line breaks inside the text with a single space. This also applies to the raw value of a field. like matches a text that has no value as an empty text.

The function replace-regex looks for matches from the start of the text to the end. It replaces each match and continues after it, so matches do not overlap. At a given place, EFX tries the alternatives from left to right. The quantifiers repeat as many times as possible, unless they are non-greedy (see Grouping and alternation and Quantifiers).

Matching always distinguishes upper case from lower case: a does not match A. No option changes this. Instead, write both forms in a character class, such as [Aa], or convert the text first with lower-case or upper-case.

Examples

BT-00-Text like '[0-9]+'

TRUE when the text contains at least one digit.

BT-00-Text like '^[0-9]+$'

TRUE when the text contains only digits, and at least one.

lower-case(BT-00-Text) like '^framework'

TRUE when the text starts with framework, in upper or lower case.

Checking of patterns

The translator checks that a pattern uses only the parts that EFX accepts.

The translator checks a pattern written as a text literal, with like or as the second argument of replace-regex, when it translates the expression. The following are errors: a part that EFX does not accept, a group or a character class that is not closed, and a quantifier that is not complete. The error message gives the position of the problem in the pattern and, in most cases, another way to write the same pattern that works in every target language.

When replace-regex receives its pattern in another way, for example from a field or a variable, the translator cannot check it. EFX uses that pattern as it is. It must follow the same rules, and an invalid pattern causes an error when the expression is applied to a notice.

The check does not find every part that EFX does not accept: see the end of Unsupported constructs.

Examples

BT-00-Text like '^[0-9]{5}$'

Accepted. The same test written ^\d{5}$ is an error when the translator translates the expression, because EFX does not accept \d.

replace-regex(BT-00-Text, BT-00-Code, '')

Not checked: the pattern is the value of a field.

EFX-1 did not check patterns. The check is new in EFX-2.

Literal characters

A literal character matches itself.

Syntax
<character>
<character><character>…
<character>

any character other than the metacharacters ., \, ?, *, +, {, }, (, ), [, ], |, ^ and $.

A literal character matches the same character in the text, and a series of literal characters matches the same series. Upper-case and lower-case letters are different characters. To match a metacharacter, write a backslash before it (see Escape sequences).

Examples

BT-00-Text like 'framework'

TRUE when the text contains framework.

BT-00-Text like '^LOT-'

TRUE when the text starts with LOT-.

See also: contains, starts-with

Wildcard

The wildcard, a full stop, matches any single character.

Syntax
.

The full stop matches any one character, except a line feed or a carriage return. Inside a character class, the full stop is a literal character. To match a full stop outside a class, write \..

Examples

BT-00-Text like '^A.C$'

TRUE for ABC, A-C or A C.

BT-00-Text like '^.{3}$'

TRUE for a text of exactly three characters.

See also: Quantifiers

Quantifiers

A quantifier, written after a part of a pattern, states how many times that part can repeat.

Syntax
<atom>*
<atom>+
<atom>?
<atom>{<n>}
<atom>{<n>,}
<atom>{<n>,<m>}
<atom>*?
<atom>+?
<atom>??
<atom>{<n>}?
<atom>{<n>,}?
<atom>{<n>,<m>}?
<atom>

the part to repeat: a literal character, the wildcard, a character class, an escape sequence or a group.

<n>, <m>

whole numbers written with digits. <n> is not greater than <m>.

The quantifiers mean:

  • *: zero or more times;

  • +: one or more times;

  • ?: zero times or once;

  • {<n>}: exactly <n> times;

  • {<n>,}: at least <n> times;

  • {<n>,<m>}: at least <n> and at most <m> times.

A quantifier applies to the atom directly before it. To repeat several characters, put them in a group: (ab)+.

The quantifiers are greedy: they match as many repetitions as possible. When you write ? after them, they become non-greedy and match as few repetitions as possible. This makes a difference for replace-regex, because it changes which parts of the text are replaced. With 'a+', aaa is one match. With 'a+?', it is three matches.

Restrictions

  • A quantifier must follow an atom. EFX does not accept a pattern that starts with *, or that has two quantifiers in a row, such as a++.

Examples

BT-00-Text like '^[0-9]{5}$'

Exactly five digits.

BT-00-Text like '^[A-Z]{2,3}-[0-9]+$'

Two or three capital letters, a hyphen and one or more digits.

replace-regex(BT-00-Text, ' +', ' ')

Replaces each run of spaces with a single space.

Character classes

A character class is a list of characters in square brackets. It matches one character of the list, or one character that is not in the list.

Syntax
[<item><item>…]
[^<item><item>…]
<item>

one of the following:

  • a character, which stands for itself;

  • a range <first>-<last>, which stands for all the characters from <first> to <last> in the order of their Unicode codes, for example a-z or 0-9;

  • an escape sequence, such as \], \-, \\ or \t (see Escape sequences);

  • a Unicode property such as \p{L} (see Unicode properties).

A class matches one character of the text that is among its items. A class whose first character is ^ matches one character that is not among its items. Inside a class, the metacharacters ., *, +, ?, (, ), {, }, | and $ are literal characters. A ^ that is not the first character, and a - that is the first or the last character, are also literal characters.

Restrictions

  • Write a ] inside a class as \]. EFX does not accept a ] without a backslash as the first character, as in []a].

  • A class must have at least one item. EFX does not accept [] and [^].

  • You must close a class with ].

Examples

BT-00-Text like '^[A-Z][a-z]+$'

A capital letter followed by one or more lower-case letters.

BT-00-Text like '^[^.]+\.[^.]+$'

Two parts without full stops, separated by one full stop.

replace-regex(BT-00-Text, '[^0-9]', '')

Removes every character that is not a digit.

BT-00-Text like '^[0-9\-]+$'

Digits and hyphens only.

Escape sequences

An escape sequence is a backslash followed by a character. It stands for a metacharacter, a white-space character or a quote.

Syntax
\<metacharacter>
\t
\n
\r
\'
\"
<metacharacter>

one of ., \, ?, *, +, {, }, (, ), [, ], |, ^ and $.

The escape sequences mean:

  • \ followed by a metacharacter: that character, as a literal character. For example, \. matches a full stop, and \\ a backslash.

  • \t: a tab.

  • \n: a line feed.

  • \r: a carriage return.

  • \' and \": a single and a double quote. These are the escape sequences of the text literal that contains the pattern. You need them when the quote is the same as the one that encloses the literal.

  • \-: a hyphen. EFX accepts this escape sequence only inside a character class.

EFX accepts no other escape sequence.

Examples

BT-00-Text like '^[0-9]+\.[0-9]{2}$'

A number with exactly two decimal places, such as 12.50.

BT-00-Text like '\(EUR\)$'

TRUE when the text ends with (EUR).

BT-00-Text like 'it\'s'

TRUE when the text contains it's.

In EFX-1, a text literal accepted only the escape sequences \d, \D, \w, \W, \s, \S, \n, \t, \r, \v, \f, \b, \c, \x, \u, \0, \', \" and \\. You could not escape a metacharacter other than the backslash, so you wrote a class such as [.] instead. EFX-1 did not turn \' and \" into quote characters.

Grouping and alternation

Parentheses group a part of a pattern, and a vertical bar separates alternatives.

Syntax
(<pattern>)
<pattern>|<pattern>
<pattern>|<pattern>|…
<pattern>

a pattern, which can be empty.

A group treats a part of a pattern as one atom, so that a quantifier applies to all of it: (ab)+ matches ab, abab and so on.

A vertical bar separates alternatives: the pattern matches when one of the alternatives matches. EFX tries the alternatives from left to right, and uses the first one that matches. So, in replace-regex, the pattern a|ab replaces only the a of ab. A group limits the reach of the alternatives. ^(EUR|USD)$ matches exactly EUR or USD, but ^EUR|USD$ matches a text that starts with EUR or ends with USD.

In replace-regex, each group also captures the part of the text that it matches. The replacement text can insert the captured parts with $1, $2 and so on, numbered in the order of the opening parentheses. You cannot refer to the captured parts inside the pattern itself.

Restrictions

  • Groups must be balanced: every ( must have its ).

  • EFX accepts only plain groups. (?:…), (?=…), (?!…), (?<=…), (?<!…), named groups and any other form that starts with (? are errors.

Examples

BT-00-Code like '^(EUR|USD|GBP)$'
BT-00-Text like '^([0-9]{3}-)*[0-9]{3}$'

Groups of three digits separated by hyphens, such as 123-456-789.

replace-regex(BT-00-Text, '([0-9]{4})/([0-9]{2})/([0-9]{2})', '$3.$2.$1')

Rewrites a date such as 2024/03/01 as 01.03.2024.

EFX-1 did not reject the group forms that start with (?.

Anchors

An anchor matches the start or the end of the text, not a character.

Syntax
^<pattern>
<pattern>$
^<pattern>$
<pattern>

a pattern.

^ matches at the start of the text, and $ at the end of the text. Line breaks inside the text do not count as a start or an end. like is TRUE when the pattern matches any part of the text. So a pattern that must describe the whole text starts with ^ and ends with $. Inside a character class, ^ as the first character means "not", and $ is a literal character.

Examples

BT-00-Text like '^[0-9]+$'

Only digits.

BT-00-Url like '^https://'

Starts with https://.

BT-00-Email like '\.eu$'

Ends with .eu.

See also: Matching

Unicode properties

A Unicode property matches a character by its Unicode category, such as letter or digit.

Syntax
\p{<category>}
\P{<category>}
<category>

the name of a Unicode general category, for example L (letter), Lu (upper-case letter), Ll (lower-case letter), N (number), Nd (decimal digit), P (punctuation), S (symbol), Z (separator) or C (control and other characters).

\p{<category>} matches a character that belongs to the category, and \P{<category>} a character that does not. The categories include the characters of all scripts. For example, \p{L} matches a, é, ß, Ω and ж, and \p{Nd} matches the digits of all scripts, not only 0 to 9. You can use Unicode properties on their own or inside a character class.

Restrictions

  • \p and \P must be followed by a category name between braces.

  • Only the names of general categories work in every target language. EFX does not accept the names of Unicode blocks, such as IsGreek.

Examples

BT-00-Text like '^\p{Lu}'

Starts with an upper-case letter, in any script.

BT-00-Text like '^[\p{L}\p{N} ]+$'

Letters, digits and spaces only.

See also: Character classes

Unsupported constructs

EFX does not accept some common parts of regular expressions. This entry lists them, each with another way to write the same pattern.

The following parts are errors in a pattern written as a text literal. The translator reports the error when it translates the expression.

Part Alternative

\d, \D (digit, non-digit)

[0-9] and [^0-9], or \p{Nd} and \P{Nd} for the digits of all scripts.

\w, \W (word character)

A class that lists the characters, such as [a-zA-Z0-9_] or [\p{L}\p{N}_].

\s, \S (white space)

[ \t\n\r] and [^ \t\n\r].

\b, \B (word boundary)

The anchors ^ and $, or a class that lists the characters.

\1 to \9 (back-reference to a group)

None. In replace-regex, the replacement text can use $1, $2, ….

(?=…), (?!…) (lookahead), (?<=…), (?<!…) (lookbehind)

None.

(?:…) (non-capturing group), (?<name>…) and (?P<name>…) (named group), and any other group that starts with (?

A plain group (…).

\0, \x…, \u… (character given by its code)

The character itself.

\p or \P without a category name between braces

\p{L}, \P{Z} and so on.

a backslash followed by any other character, such as \a, \i or \/, and \- outside a character class

An escape sequence that EFX accepts, or the character itself.

a [ or a ( without its closing ] or ), a ) without its opening (

Balanced brackets and parentheses, or the escape sequences \[, \], \( and \).

a repetition that is not complete, such as {2 or {2,x}

{2}, {2,} or {2,5}, or \{ for a brace.

EFX does not accept the following parts either. They cause an error when the expression is applied to a notice, or they do not give the same result in all target languages. The translator does not report them.

  • \f (form feed);

  • a { or a } that is not part of a quantifier: write \{ and \};

  • a ] outside a character class, or as the first character of a class: write \];

  • two quantifiers in a row, as in the possessive quantifiers a*+ and a++;

  • the subtraction of classes, as in [a-z-[aeiou]];

  • Unicode properties that name a block, such as \p{IsGreek}.

Examples

BT-00-Text like '^[0-9]{5}$'

The form of ^\d{5}$ that works in every target language.

replace-regex(BT-00-Text, '[ \t\n\r]+', ' ')

The form of replace-regex(BT-00-Text, '\s+', ' ') that works in every target language.

EFX-1 accepted these parts without checking them.