Omitting Subexpressions From Backreferences; Returning Matched Subexpressions - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

For example, the following statement replaces the uppercase string "HELLO" with a lowercase
"hello". This example uses backreferences to perform the replacement. For more information on
using backreferences, see
reReplace("HELLO", "([[:upper:]]*)", "Don't shout\scream \L\1")
The result of this example is the string "Don't shout\scream hello".
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
match of the regular 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
functions multiple times.
Both the
REFind
starting index in the 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
starting index of 8:
<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 ".
154
Chapter 7: Using Regular Expressions in Functions
"Using backreferences in replacement strings" on page
functions return the location in the search string of the first
REFindNoCase
and
functions take an optional third parameter that specifies the
REFindNoCase
153.
and
REFind
REFindNoCase
REFind
with a

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

This manual is also suitable for:

Coldfusion mx

Table of Contents