# Regex Model Syntax

The regex model uses the following syntax:

Code | Definition
--- | ---
. | Matches any character, except for space.
[A-Za-z] | Matches any English character. See "Character classes" below.
[^A-Z] | Matches any character, except for space and the upper case characters.
\d, \u | Equivalent to [0-9] and [A-Z] respectively.
jan|feb | Matches either "jan" or "feb"
() | Groups characters. Useful if followed by ?, *, +, or {5}, or to delimit the range of or an either/or expression using |.
space | Space is special. It does not match any feature, but tells the Blue Read tool's model matcher to expect a longer gap between features here, which affects the scoring.
\*, \?, \+, \(, \), \[, \], \{, \}, \., \|, \\ | Escapes a literal control character.
? | The preceding expression is optional.
* | The preceding expression repeats 0 or more times.
+ | The preceding expression repeats 1 or more times.
{5} | The preceding expression repeats exactly 5 times.
{5,8} | The preceding expression repeats 5 to 8 times, inclusive.
Other character | Matches that character literally.

Inside of character classes (for example [a-z]) the following syntax rules apply:

- A - denotes a range, unless it occurs at the beginning of the class specification (possibly after ^) or at the end. If you need a literal -, place it in one of those locations.
- A closing square bracket ] ends the class, unless it is escaped with \, or it occurs at the very beginning of the class specification (possibly after ^).

- If you want a backslash, you should escape it with another backslash.

  Note: The only characters with special meaning are ], -, and \. Any other character (for example *, ?, ), |, etc) have their literal meanings, and should not be escaped.

### Space in a Regex String

You use space (blank) characters in the regex string to indicate expected gaps between characters. If you specify a large gap the tool may skip over characters during matching. A general guideline is to use a space character to indicate a gap that is as large as the feature size you've specified.

As described in the Regex Model Syntax topic, a (normal ASCII) space character is special. It does not match a feature, but tells the model matcher to expect a bigger gap between features at that position. You can add several spaces if wider gaps are expected. The final "match string" for the model will include those spaces.

Space is the only character that is special. Other whitespace characters simply try to match a feature which is labeled with that particular character.

Note: The regex model does not support any "multi-line" models. CR or LF characters have no special meaning.

### Unicode Character Matching for Regex Model

The regex model supports matching Unicode characters, with the caveat that it operates in units of Unicode code points – each feature is assumed to correspond to a Unicode code point.

Note: In Unicode, there are so-called "combining characters", which means that a character can consist of several code points. This is a common way to express unusual combinations of diacritics.

Unicode provides so-called precomposed characters – characters (even with diacritics) which only consist of a single code point. To ensure that these are used, the user should use the Unicode normalization form NFC. (Notable exceptions are half-width Japanese kana with dakuten or handakuten diacritics – these have no precomposed form in Unicode.)

### Regex Models and Empty Features

When labeling characters for training, after a labeled feature has been added, but before it has been given a character, it exists in a special "empty" state. As a special case, the regex model will always be able to match empty features, regardless of the character class or literal character used. This helps with work flows where you are using the model to be able to enter the ground truth for many features as one string.