RegEx Support in ColdFusion


Now that you have an idea of what regular expressions are, you need to understand what kind of support ColdFusion provides for them. The basic facts are these:

  • The syntax you can use in your regular expressions (that is, the wildcards and such) is nearly identical to the syntax supported in Perl.

  • In ColdFusion, you use the reFind() and reReplace() functions to perform a RegEx operation. This is in contrast to the way regular expressions are invoked in Perl or JavaScript, which allow you to sprinkle them throughout your code almost as if the expressions were ordinary strings.

What Changed in ColdFusion

As I mentioned earlier, CFML's support for regular expressions was improved greatly for ColdFusion. RegEx enthusiasts who found the support lacking in previous versions are much happier now. The most important improvements are listed in Table 13.1.

Table 13.1. RegEx Features Supported in ColdFusion

FEATURE

DESCRIPTION

Escape sequences

ColdFusion now supports nearly all the special escape sequences that Perl does. This means you can use \n for end-of-line characters, \t for tabs, and so on. Refer to Table 13.13 for the list of escape sequences for special characters.

Minimal (non-greedy) matches

ColdFusion allows you to create regular expressions that find the least amount of text possible, rather than the most amount of text. The ColdFusion documentation calls this minimal matching, which is a way to specify what RegEx people usually call non-greedy matching. See Table 13.9 for details.

Word boundaries

ColdFusion allows you to use the \b and \B word boundary meta characters, generally used for matching whole words or pieces of words. See Table 13.10 for details.

Lookahead matching

ColdFusion implements positive and negative lookahead processing via Perl's (?=) and (?!) modifiers. See Table 13.12 for details.

Multiline mode

ColdFusion supports the (?m) multiline mode sequence introduced by Perl 5. See Table 13.12 for details.


Together, these additions make ColdFusion's RegEx support much more powerful, and much closer to the way regular expressions work in Perl, which for many people is the de facto standard for how regular expressions should behave.

NOTE

To a large extent, we have Daniel Savarese and the other kind folks involved in the Jakarta ORO project to thank for the RegEx improvements in ColdFusion. For details, check out the ORO project home page at http://jakarta.apache.org/oro.


Where Can You Use Regular Expressions?

You still haven't learned how to construct these strange-looking regular expression things, but assuming you have one of them already (such as the <[^>]*> or ([\w._]+)\@([\w_]+(\.[\w_]+)+) expressions that I mentioned earlier), you might be wondering where you can use them. In Perl, you tell the engine that a string should be interpreted as a regular expression by delimiting it with / characters, optionally adding additional "switches" to control options such as case sensitivity. That wouldn't work so well in CFML, due to its tag-based nature. Instead, you use the special set of RegEx functions, listed in Table 13.2.

Table 13.2. ColdFusion's RegEx Functions

FUNCTION

DESCRIPTION

reFind()

Attempts to find a match for a regular expression within a block of text. It's similar conceptually to the normal find() function, except that the string you're looking for can include regular expression wildcards.

reFindNoCase()

Same as reFind(), except that the matching ignores capitalization.

reReplace()

Finds matches within a block of text, replacing the matches with whatever replacement string you specify. You can use special characters in the replacement string to pull off all sorts of fancy replacement tricks.

reReplaceNoCase()

Same as reReplace(), except performing the matching without respect to capitalization.


You'll learn how to use these functions next.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net