Installation Manual

Table Of Contents
Chapter 4. Design 105
© 2018 Bodno
occurrences to 2, so the Validation Rule becomes [0-9]{1,2}. This Validation
Rule satisfies the original requirement to match integers from 0 to 99, but it will
also match integers that occur in the middle of strings. If we want the matched
integer to be the whole string, we must use the anchor assertions, ^ (caret) and
$ (dollar). When ^ is the first character in a Validation Rule, it means the
Validation Rule must match from the beginning of the string. When $ is the last
character of the Validation Rule, it means the Validation Rule must match to the
end of the string. The Validation Rule becomes ^[0-9]{1,2}$. Note that
assertions, e.g. ^ and $, do not match characters but locations in the string.
If you have seen Validation Rules described elsewhere, they may have looked
different from the ones shown here. This is because some sets of characters
and some quantifiers are so common that they have been given special symbols
to represent them. [0-9] can be replaced with the symbol \d. The quantifier to
match exactly one occurrence, {1,1}, can be replaced with the expression itself,
i.e. x{1,1} is the same as x. So our 0 to 99 matcher could be written as
^\d{1,2}$. It can also be written ^\d\d{0,1}$, i.e. From the start of the string,
match a digit, followed immediately by 0 or 1 digits. In practice, it would be
written as ^\d\d?$. The ? is shorthand for the quantifier {0,1}, i.e. 0 or 1
occurrences. ? makes an expression optional. The Validation Rule ^\d\d?$
means From the beginning of the string, match one digit, followed immediately
by 0 or 1 more digit, followed immediately by end of string.
To write a Validation Rule that matches one of the words 'mail' or 'letter' or
'correspondence' but does not match words that contain these words, e.g.,
'email', 'mailman', 'mailer', and 'letterbox', start with a Validation Rule that
matches 'mail'. Expressed fully, the Validation Rule is m{1,1}a{1,1}i{1,1}l{1,1},
but because a character expression is automatically quantified by {1,1}, we can
simplify the Validation Rule to mail, i.e., an 'm' followed by an 'a' followed by an
'i' followed by an 'l'. Now we can use the vertical bar |, which means or, to
include the other two words, so our Validation Rule for matching any of the three
words becomes mail|letter|correspondence. Match 'mail' or 'letter' or
'correspondence'. While this Validation Rule will match one of the three words
we want to match, it will also match words we don't want to match, e.g., 'email'.
To prevent the Validation Rule from matching unwanted words, we must tell it to
begin and end the match at word boundaries. First we enclose our Validation
Rule in parentheses, (mail|letter|correspondence). Parentheses group
expressions together, and they identify a part of the Validation Rule that we wish
to capture. Enclosing the expression in parentheses allows us to use it as a
component in more complex Validation Rules. It also allows us to examine which
of the three words was actually matched. To force the match to begin and end
on word boundaries, we enclose the Validation Rule in \b word boundary
assertions: \b(mail|letter|correspondence)\b. Now the Validation Rule means:
Match a word boundary, followed by the Validation Rule in parentheses, followed
by a word boundary. The \b assertion matches a position in the Validation Rule,
not a character. A word boundary is any non-word character, e.g., a space,
newline, or the beginning or ending of a string.