COMPILE Function

Compiles a valid regular expression using the specified encoding.

Category: Regular Expression
Returned data type: Integer

Syntax

r.COMPILE(regex <,encoding>)

Required Argument

regex

specifies a Perl-compatible regular expression

Optional Argument

encoding

specifies a string that defines the encoding constant shown Encoding. Use a value from the Encoding column

  Default The default encoding for the operating system.

Details

The COMPILE function is used with a regular expression object. You must define a regular expression object first as a variable before you can use COMPILE( ) to compile a PERL-compatible regular expression. Regular expressions can be used to do advanced pattern matching (and in some cases pattern replacement). Use the other functions listed below to find patterns in a given string (which can be a variable), to determine the length of matching patterns, and to replace patterns.

The function returns 1 if the regular expression compilation is successful. Otherwise, it returns 0. Failure could be due to an incorrectly formatted regular expression or possibly an invalid encoding constant.

For performance reasons, it is best to compile a regular expression in a preprocessing step in the Expression node. This means that the regular expression is compiled just once before data rows are processed by the Expression node.

Note: The sample code in this section generally places the compile() function on the expression tab with the rest of the expression code for clarity.

In some cases, you might need to have the regular expression compiled before every row is evaluated. For example, you can use a variable to define the regular expression that you want to compile. The variable might come from the data row itself, and you would need to recompile the regular expression for each row to have the pattern searching work correctly.

Take care to design regular expressions that find patterns only for which you want to search. Poorly written regular expression code can require a lot of additional processing that can negatively impact performance.

Example

Note: This example can be run in a stand-alone expression node if the Generate rows when no parent is specified option is selected. If passing data to this node, turn this setting off and remove the SETEOF() function. Unless stated otherwise, all code shown should be entered in the Expression tab of the Expression node.

//You must define a regex object
regex r
//Then compile your regular expression
//This example will match any single digit in an input string
r.compile ("[0-9]","ISO-8859-1")
// Terminate the expression node processing
seteof()