Download Regex - the FreeBSD Documentation Server
Transcript
Chapter 7: Programming with Regex
33
initialize the structure before passing it. Much existing code doesn't do this initialization, and it's
arguably better to avoid it anyway.
sets regs_allocated to REGS_UNALLOCATED, so if you use the GNU regular expression functions, you get this behavior by default.
re_compile_pattern
xx document re set registers
posix, on the other hand, requires a dierent interface: the caller is supposed to pass in a xed-
length array which the matcher lls. Therefore, if
simply lls that array.
regs_allocated
is REGS_FIXED the matcher
The following examples illustrate the information recorded in the re_registers structure. (In
all of them, `(' represents the open-group and `)' the close-group operator. The rst character in
the string string is at index 0.)
If the regular expression has an i-th group not contained within another group that matches
a substring of string, then the function sets regs->start[i ] to the index in string where the
substring matched by the i-th group begins, and regs->end[i ] to the index just beyond that
substring's end. The function sets regs->start[0] and regs->end[0] to analogous information
about the entire pattern.
For example, when you match `((a)(b))' against `ab', you get:
0 in regs->start[0] and 2 in regs->end[0]
0 in regs->start[1] and 2 in regs->end[1]
0 in regs->start[2] and 1 in regs->end[2]
1 in regs->start[3] and 2 in regs->end[3]
If a group matches more than once (as it might if followed by, e.g., a repetition operator), then
the function reports the information about what the group last matched.
For example, when you match the pattern `(a)*' against the string `aa', you get:
0 in regs->start[0] and 2 in regs->end[0]
1 in regs->start[1] and 2 in regs->end[1]
If the i-th group does not participate in a successful match, e.g., it is an alternative not taken
or a repetition operator allows zero repetitions of it, then the function sets regs->start[i ]
and regs->end[i ] to ,1.
For example, when you match the pattern `(a)*b' against the string `b', you get:
0 in regs->start[0] and 1 in regs->end[0]
,1 in regs->start[1] and ,1 in regs->end[1]