beautypg.com

Rules for building regular expressions – Visara Master Console Center Scripting Guide User Manual

Page 56

background image

Chapter 4 Regular Expressions

Scripting Guide

56

Rules for Building Regular Expressions

When using the rules to build regular expressions (REs), first determine
which substrings are matched by a pattern. Consider the following RE
“c.*n”. This pattern matches a substring beginning with c, ending with n,
and with any number of characters between, including none. This pattern
matches strings containing cn, cxn, and collision.
The following three strings are used to illustrate the discussion of REs:

String 1:

“ab”

String 2:

“acbcbc”

String 3:

“12356”

As an introductory example, if the RE “b” is processed on each string, the
results are:

String 1:

match

String 2:

match

String 3:

no match

The RE “b”, the pattern, matches the letter b in the first and second
strings, and there is no b in the third string.
The RE “c” would match just the second string.
The RE “bc”, built by concatenating the prior two REs, would match just
the second string — there are two instances of bc in the second string.
A null pattern will match any character, so the RE “” matches all three
strings.
The search for a match starts at the beginning of a string, scans from left
to right, and stops when it finds the first sequence matching the pattern.
If there is more than one possible leftmost match, the longest match is
used. For example, in the following expression, the pattern c.*n matches
the second through third characters and also the second through the fifth
characters of the second string:

acncnc

The second match, being the longer in length, may be the desired match.
However, a longer substring that is not the leftmost match is not the
actual match.
A multi-character collating element is considered a single character that
describes how to form a bracket expression. Bracket expressions are used
to match a single character. However, when considering what is the
longest sequence in a match involving a multi-character collating
element, the element counts not as one character but as the number of
characters it matches.
Pattern matching can search for case-insensitive strings. Case-
insensitive processing permits matching of multi-character collating
elements as well as characters. For example, the RE “[[.Ch.]]” would
match ch, Ch, cH, or CH.