Regular Expressions Gotcha

I was playing with some code the other day and ran into a 'gotcha' that I should have seen. I knew it in a different situation and just didn't apply it to what I was doing at the time.

Basically, ColdFusion can store a Regular Expression in a variable and then use that variable within a REFind/REReplace function as if the expression was written there. In other words, ColdFusion evaluates the variable, sees that it is in the Regular Expression position of the function and uses it as the expression to parse.

If this is so well known, then what was the problem? I was doing a search for a piece of text and then use a REReplace function to replace the text I found with a different piece of text. Straightforward and all but the text that I found had a parenthesis in it (an open and a close to be exact). This means that it was a valid Regular Expression and was to be parsed. All I wanted to do was use it as the replace text, but ColdFusion wanted to do something else with it.

Bottom line is that after beating my head against the wall looking at the data I ran a few tests and then a few more and saw the issue. Makes me feel really foolish when I can't apply a known gotcha to an unknown problem. :(

Here's the code to showcase the issue:

<cfset String="this is trim(name) which is a variable inside a function">
<cfset Searchtext="trim(name)">
<cfset Position=REFind(Searchtext, String)>
<cfset String=REReplace(String, Searchtext, '<b>\1</b>')>
<cfoutput>#Position#-#String#</cfoutput>
The position returned will be 0 and there will be no replace as the text that is looked for is not trim(name) but trimname. The parenthesis in Regular Expressions are special characters to allow grouping.

Because this was a REReplace and the text was coming from a search, I didn't see the text, didn't realize that it was a valid Regular Expression and didn't realize that it would fail (Variable stored Regular Expressions are almost always used in REFind examples). It's times like this that I could use a really good debugger to see what's happening in each variable as it changes. Luckily, that debugger should be out in a few weeks.

FusionDebug

IsValid and RegEx Issues

While writing some new documentation on ColdFusion MX 7 features you may have missed (for the soon to be released Fusion Authority Quarterly Update), I came across an interesting issue with the IsValid function.

This function is used to test if a ColdFusion variable is of a particular data type or if it is a particular value (email address, credit card, etc). In many ways this is a single function that can stand in for most of the standard "IS" functions (IsStruct, IsQuery, etc.) One noted exception is that the function has no existance checking (IsDefined).

One feature of the function is the ability to use a regular expression as a validator. This makes the IsValid tag a perfect replacement for using REFind to do regular expression pattern validation. That is, with a single exception. REFind is case sensitive and has a NoCase version to deal with non-case sensitive matches. IsValid is only case sensitive.

For many, this may mean that using IsValid in place of REFind for regular expression pattern validation is not an option. To those I say, there is a simple solution.

ColdFusion MX 7 has some advanced options for regular expressions which include a simple prefix for any expression to make it case insensitive. To make IsValid in regular expression mode case insensitive, just do the following:

IsValid('regex', variable, '(?i)pattern')

The (?i) syntax at the start of a regular expression pattern makes the entire match case insensitive. Other advanced regular expression prefixes can be used with the function as well, which can greatly expand its capabilities.

Note that we are talking about the ColdFusion-style regular expressions here. The documentation for the IsValid function incorrectly states that it uses Javascript-style regular expression syntax. This is not the case.

There are a few other quirks with the function that you're almost never going to run into and I suggest you keep an eye on the IsValid LiveDocs entry for any changes and clarifications.

BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner
House of Fusion | ColdFusion Jobs @ House of Fusion | Fusion Authority