Regular Expressions
|
Introduction
This tutorial presents a rudimentary explanation of regular expressions (RE's)
and their use in matching and replacing text found in rib files. Fortunately,
rib files have a consistent structure that makes writing RE's relatively straightforward.
For example, an RE that would match the following statement in a rib
file can take advantage of the fact the values associated with the Illuminate 2 1 Although the examples in this tutorial, written in Tcl, assume the text that is being searched/replaced are rib statements originating from a rib file the code that deals with the opening, reading and closing of text files has been omitted. Details about file handling can be found in the tutorial "Tcl: File Filtering" There are two main procedures in Tcl that use RE's, namely, regsub OPTIONS REGULAR_EXPRESSION text_in text_replace text_out regexp OPTIONS REGULAR_EXPRESSION text_in text_out Examples 1 and 2 demonstrate how these procedures are used. |
Example 1 regsub
The text shown in bold specifies a regular expression, called a pattern, consisting of
literal characters that hello student For information about the execution of Tcl scripts with Cutter refer to the tutorial "Cutter: About" Rib Files & ShadingRate
Rib files produced by Maya (mtor and RfM Pro) and Houdini have one or more
# integer format ShadingRate 2 # decimal format ShadingRate 0.5 # expotential format - possible but highly unlikely! ShadingRate 9.59616e-017 Expotential format will be ignored for the purposes of this tutorial. Instead of dealing directly with a rib file, assume we have a single line of text consisting of, ShadingRate 5 that must be changed to, ShadingRate 10 This could be accomplished as follows. Example 1b
The pattern means, match ShadingRate, followed by a digit However, the pattern works only if the value of ShadingRate is a single digit. This input will cause the pattern to fail! ShadingRate 23 To specify one or more digits the regular expression counting qualifier "+" can be used. Example 1c
The pattern means, match ShadingRate, followed by one or more digits But this input will cause the pattern to fail! ShadingRate 2.5
The pattern could be changed to {ShadingRate \d+.\d+}.
While this pattern will appear to be successful it is so only because the period is a
regular expression metacharacter that means "match any character" following the
period.
Example 1d
The pattern means, match ShadingRate, followed by zero or more digits, followed by an optional decimal point, folowed by zero or more digits
The pattern uses the counting qualifiers "*" and "?". Unfortunately,
the pattern is flawed because it defines sub-components of the number pattern as
optional. As a result it doesn't even require a numeric value
in order to achieve a match!
Example 1e
The pattern means, match ShadingRate, followed by zero or more digits, followed by either, an optional decimal point and zero or more digits OR a decimal point and at least one digit |
Example 2 regexp
The procedure Student, how are you? The documentation on this proecedure can be displayed in Cutter by alt + double clicking on the "regexp". Refer to the tutorial "Cutter: Integration with Tcl". Rib Files & Surface Shading
Rib files generally contain one or more Surface "foo" "float bar" [1.0] "float Kd" [0.5] Suppose the value of the "bar" parameter must be altered to 2.5. Although somewhat unlikely, it is possible that another, entirely different, shader may use a parameter with the same name. Therefore, before a Python or Tcl script attempts to substitute a value for "bar" it must check if the parameter "belongs" to "foo". For example, Example 2b
|
The
|
© 2002- Malcolm Kesson. All rights reserved.