Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 146

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
Escaping special characters in replacement strings
You use the backslash character, \, to escape backreference and case-conversion characters in replacement strings. For
example, to include a literal "\u" in a replacement string, escape it, as in "\\u".
Omitting subexpressions from backreferences
By default, a set of parentheses will both group the subexpression and capture its matched text for later referral by
backreferences. However, if you insert "?:" as the first characters of the subexpression, ColdFusion performs all
operations on the subexpression except that it will not capture the corresponding text for use with a back reference.
This is useful when alternating over subexpressions containing differing numbers of groups would complicate
backreference numbering. For example, consider an expression to insert a "Mr." in between Bonjour|Hi|Hello and
Bond, using a nested group for alternating between Hi & Hello:
<cfset regex = "(Bonjour|H(?:i|ello))( Bond)">
<cfset replaceString = "\1 Mr.\2">
<cfset string = "Hello Bond">
#REReplace(string, regex, replaceString)#
This example returns "Hello Mr. Bond". If you did not prohibit the capturing of the Hi/Hello group, the \2
backreference would end up referring to that group instead of " Bond", and the result would be "Hello Mr.ello".
Returning matched subexpressions
The
and
REFind
REFindNoCase
expression. Even though the search string in the next example contains two matches of the regular expression, the
function only returns the index of the first:
<cfset IndexOfOccurrence=REFind(" BIG ", "Some BIG BIG string")>
<!--- The value of IndexOfOccurrence is 5 --->
To find all instances of the regular expression, you must call the
Both the
and
REFind
REFindNoCase
search string for the search. By default, the starting location is index 1, the beginning of the string.
To find the second instance of the regular expression in this example, you call
<cfset IndexOfOccurrence=REFind(" BIG ", "Some BIG BIG string", 8)>
<!--- The value of IndexOfOccurrence is 9 --->
In this case, the function returns an index of 9, the starting index of the second string " BIG ".
To find the second occurrence of the string, you must know that the first string occurred at index 5 and that the string's
length was 5. However,
REFind
length of the matched string to call
The
and
REFind
REFindNoCase
functions' fourth parameter,
and
, containing the positions and lengths of text strings that match the subexpressions of a regular expression,
pos
len
as the following example shows:
<cfset sLenPos=REFind(" BIG ", "Some BIG BIG string", 1, "True")>
<cfoutput>
<cfdump var="#sLenPos#">
</cfoutput><br>
functions return the location in the search string of the first match of the regular
functions take an optional third parameter that specifies the starting index in the
only returns starting index of the string, not its length. So, you either must know the
the second time, or you must use subexpressions in the regular expression.
REFind
functions let you get information about matched subexpressions. If you set these
, to True, the functions return a CFML structure with two arrays,
ReturnSubExpression
Last updated 1/20/2012
and
REFind
REFindNoCase
with a starting index of 8:
REFind
141
functions multiple times.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents