rexCompile
rexCompile(t_pattern) =>t/nil
Description
Compiles a regular expression string pattern into an internal representation to be used by succeeding calls to rexExecute.
This allows you to compile the pattern expression once using rexCompile and then match a number of targets using rexExecute; this gives better performance than using rexMatchp each time.
rexCompile does not support the extended regular expression syntax. To parse such regular expressions, you can use the pcre (Perl Compatible Regular Expressions) functions (such as pcreCompile) instead.Arguments
Value Returned
|
Signals an error if the given pattern is ill-formed or not a legal expression. |
Examples
rexCompile("^[a-zA-Z]+")
=> t
rexCompile("\\([a-z]+\\)\\.\\1")
=> t
rexCompile("^\\([a-z]*\\)\\1$")
=> t
rexCompile("[ab")
=> *Error* rexCompile: Missing ] - "[ab"
Pattern Matching of Regular Expressions
In many applications, you need to match strings or symbols against a pattern. SKILL provides a number of pattern matching functions that are built on a few primitive C library routines with a corresponding SKILL interface.
A pattern used in the pattern matching functions is a string indicating a regular expression. Here is a brief summary of the rules for constructing regular expressions in SKILL:
How Pattern Matching Works
The mechanism for pattern matching
- Compiles a pattern into a form and saves the form internally.
- Uses that internal form in every subsequent matching against the targets until the next pattern is supplied.
The rexCompile function does the first part of the task, that is, the compilation of a pattern. The rexExecute function takes care of the second part, that is, matching a target against the previously compiled pattern. Sometimes this two-step interface is too low-level and awkward to use, so functions for higher-level abstraction (such as rexMatchp) are also provided in SKILL.
Avoiding Null and Backslash Problems
- A null string ("") is interpreted as no pattern being supplied, which means the previously compiled pattern is still used. If there was no previous pattern, an error is signaled.
- To put a backslash character (\) into a pattern string, you need an extra backslash (\) to escape the backslash character itself.
For example, to match a file name with dotted extension .il, the pattern "^[a-zA-Z]+\\.il$" can be used, but "^[a-zA-Z]\.il$" gives a syntax error. However, if the pattern string is read in from an input function such as gets that does not interpret backslash characters specifically, you should not add an extra backslash to enter a backslash character.
Related Topics
Return to top