User Guide

Table Of Contents
148 Chapter 7: Using Regular Expressions in Functions
You must be aware of the following considerations when using special characters in character sets,
such as [a-z]:
To include a hyphen (-) in the square brackets of a character set as a literal character, you
cannot escape it as you can other special characters because ColdFusion always interprets a
hyphen as a range indicator. Therefore, if you use a literal hyphen in a character set, make it
the last character in the set.
To include a closing square bracket (]) in the character set, escape it with a backslash, as in [1-
3\]A-z]. You do not have to escape the ] character outside of the character set designator.
Using escape sequences
Escape sequences are special characters in regular expressions preceded by a backslash (\). You
typically use escape sequences to represent special characters within a regular expression. For
example, the escape sequence \t represents a tab character within the regular expression, and the
\d escape sequence specifies any digit, similar to [0-9]. In ColdFusion the escape sequences are
case-sensitive.
(?=...) If at the beginning of a regular expression, it specifies to use positive lookahead when
searching for the regular expression.
Positive lookahead tests for the parenthesized subexpression like regular parenthesis,
but does not include the contents in the match - it merely tests to see if it is there in
proximity to the rest of the expression.
For example, consider the expression to extract the protocol from a URL:
<cfset regex = "http(?=://)">
<cfset string = "http://">
<cfset result = reFind(regex, string, 1, "yes")>
mid(string, result.pos[1], result.len[1])
This example results in the string "http". The lookahead parentheses ensure that the "://
" is there, but does not include it in the result. If you did not use lookahead, the result
would include the extraneous "://".
Lookahead parentheses do not capture text, so backreference numbering will skip over
these groups. For more information on backreferencing, see “Using backreferences”
on page 150.
(?!...) If at the beginning of a regular expression, it specifies to use negative lookahead.
Negative is just like positive lookahead, as specified by (?=...), except that it tests for the
absence of a match.
Lookahead parentheses do not capture text, so backreference numbering will skip over
these groups. For more information on backreferencing, see “Using backreferences”
on page 150.
(?:...) If you prefix a subexpression with "?:", ColdFusion performs all operations on the
subexpression except that it will not capture the corresponding text for use with a back
reference.
Special
Character
Description