B: %b/manif/pmatch ret = p.compile(pcode,pattern, [optbits,patlen] );
C: #include <pmatch.h> int p_compile(pm_code **pcode, const char *pattern, [int opts, int *patlen]);
status = p.compile(&pcode, "^^abc$"); if (0 > p.match(line, pcode)) error("line did not match*n");
P.COMPILE "compiles" a pattern. The result is executable code that can be used to determine whether or not any string contains a substring matching the pattern. Memory for the compiled code is allocated dynamically, and a handle is returned in the location pointed to by "pcode".
Actual pattern matching is done by passing this handle to the P.MATCH function. The memory used by the pattern code is released by calling P.FREE.
If the compilation fails, no memory is allocated, and the value stored at "*patlen" can be used to determine the offset into the pattern string where the error was detected.
The simplest pattern is just a string of normal characters. For example, "abc" matches the sequences 'a', 'b', 'c' anywhere in a string. By default, pattern matching ignores the case of letters, but you can change this by using the P.OPT function to turn off the 'd' special character option.
Patterns can also contain special characters. The special characters that can be used with P.COMPILE are a subset of the special characters recognized by the FRED text editor. Below, we summarize the special pattern characters that P.COMPILE recognizes.
. -- any single character (except new-line) ^ -- start of line $ -- end of line P* -- zero or more of pattern P P+ -- one or more of pattern P P|Q -- pattern P or Q (P) -- same as pattern P [XYZ...] -- any character inside brackets [^XYZ...] -- any character not inside brackets {P}T -- pattern P with tag T < -- beginning of word > -- end of word @(N) -- null string before column N @(-N) -- null string after Nth last column
Complete explanations of these are given in "expl fred pattern" and in the FRED Reference Manual. P.COMPILE does NOT recognize the "fence" pattern character '#'. Since P.COMPILE doesn't do anything special with tagged patterns, the @T constructs are not supported. In addition, P.COMPILE does not recognize the '-' shorthand inside "[]".
By default, the special meanings of
$^.*[]{}|()
are turned on, while the special meanings of
<>@+
are turned off.
Putting "\c" in front of a character turns off its special meaning, while putting "\o" in front of a character turns on its special meaning. For example,
"\c["
is a pattern matching any string that contains an opening square bracket. The special meaning of "[" is ignored.
Special characters can also be turned on or off by calling the P.OPT function. You must do this BEFORE you call P.COMPILE to compile the pattern. Once a pattern has been compiled, turning meanings on or off does not affect the compiled pattern's behavior.
You can represent non-printable or "hard to type" characters with "\ddd" where "ddd" are octal digits giving the ASCII representation of the character you wish to match. For example,
"\007"
matches any string that contains the BELL character.
In constructs like "\c" or "\007", the first character is called the "escape character". By default, the escape character is "\"; you can change this by calling the P.OPT function.
Normally, the pattern is terminated by a '*0' byte, but you can define other termination characters using P.OPT. This can be useful if you are parsing a string like "s/ab\c/def/xyz/". After scanning the initial "s/", you can call P.OPT to set '/' as a termination character, then pass the "ab\c/def/xyz/" sequence to P.COMPILE. P.COMPILE stops compiling the pattern after the 'f'. You can then use the value stored in "*patlen" to find the remainder of the string.
Copyright © 1996, Thinkage Ltd.