Sorcerer's IsleDocs cfRegexOverviewCode

Regex Object

Whenever any regex action is performed, there is a required step of processing the textual regex pattern and producing the 'machine' that actually applies the instructions provided by the pattern.

If the regex is a single use one, this is simply an unavoidable step, but when a regex is going to be used more than once, it is inefficient to repeat this identical event multiple times.

To help solve this problem, cfRegex introduces a Regex Object, allowing the regex pattern and modes to be compiled once into a variable, which can then be re-used as many times as necessary, with different actions, input text, and/or parameters.

This can allow both performance improvements and greater readability.

Creating Regex Objects

There are multiple ways to create a regex object. Typically, this can be done using the new keyword, like so:

<cfset MyRegex = new Regex( pattern , modes ) />

Or also with the RegexCompile function, which works similarly:

<cfset MyRegex = RegexCompile( pattern , modes ) />

When compiling a more complicated regex pattern, you can use the cfregex tag syntax, which is especially suited for splitting long regex across multiple lines:

<cfregex name="MyRegex" modes="modes">

For all three of these examples, only pattern is required - the modes are optional. For the tag syntax, the comment mode is on by default, which means whitespace is ignored (except in some situations, see the documentation on comment mode for full details).

Using Regex Objects

The use of a Regex Object is the same as with any other object, as in:

<cfset Result = MyRegex.action( Arguments ) />

Where action is any of the methods on the object, as listed on the Actions page.