### The “RexPattern” Intrinsic Class

A RexPattern object stores the internal representation, known as the “compiled” version, of a regular expression pattern. The internal details of the compiled representation aren’t important, and the program can’t access the compiled data directly.

Every time the program performs a search involving a regular expression (using the rexMatch, rexSearch, or rexReplace functions), the system must work with the compiled form of the search pattern. The simplest way to call these functions is to pass them a string giving the search pattern, but when the program does this, the system must compile the string, which converts the string to the internal representation. This compilation process is relatively time-consuming; typically, compiling the pattern takes about half the time involved in performing a regular expression search.

The purpose of the RexPattern class is to let the program perform this compilation work just once for a given pattern string, and then re-use the same compiled representation every time the program searches for the pattern. If a given pattern is used repeatedly, this can improve the program’s efficiency by avoiding repeated compilation of the same pattern string.

### RexPattern Creation

Creating a RexPattern object is simple: just use the “new” operator to create the object, giving the pattern string as the argument:

      local pat = new RexPattern('a.*b');

This creates the pattern object and compiles the pattern string. You can now use the object in the regular expression search and match functions (rexMatch, rexSearch, rexReplace) in place of the pattern string. The functions will behave exactly as though you had used the original pattern string, except that they will run somewhat more quickly, because they won’t need to compile the string.

### Methods

The class provides the standard Object intrinsic class methods, plus the following:

getPatternString() – returns the original string used to create the object (i.e., the string passed as the argument in the “new” expression that created the object).