About regular expressions
In traditional string matching, as used by the ColdFusion
provide the string pattern to search for and the string to search. The following example searches a
string for the pattern " BIG " and returns a string index if found. The string index is the location
in the search string where the string pattern begins.
<cfset IndexOfOccurrence=Find(" BIG ", "Some BIG string")>
<!--- The value of IndexOfOccurrence is 5 --->
You must provide the exact string pattern to match. If the exact pattern is not found,
an index of 0. Because you must specify the exact string pattern to match, matches for dynamic
data can be very difficult, if not impossible, to construct.
The next example uses a regular expression to perform the same search. This example searches for
the first occurrence in the search string of any string pattern that consists entirely of uppercase
letters enclosed by spaces:
<cfset IndexOfOccurrence=REFind(" [A-Z]+ ", "Some BIG string")>
<!--- The value of IndexOfOccurrence is 5 --->
The regular expression " [A-Z]+ " matches any string pattern consisting of a leading space,
followed by any number of uppercase letters, followed by a trailing space. Therefore, this regular
expression matches the string " BIG " and any string of uppercase letters enclosed in spaces.
By default, the matching of regular expressions is case-sensitive. You can use the case-insensitive
functions,
REFindNoCase
Because you often process large amounts of dynamic textual data, regular expressions are
invaluable in writing complex ColdFusion applications.
Using ColdFusion regular expression functions
ColdFusion supplies four functions that work with regular expressions:
•
REFind
•
REFindNoCase
•
REReplace
•
REReplaceNoCase
and
REFind
REFindNoCase
string index where it finds the pattern. For example, the following function returns the index of
the first instance of the string " BIG ":
<cfset IndexOfOccurrence=REFind(" BIG ", "Some BIG BIG string")>
<!--- The value of IndexOfOccurrence is 5 --->
To find the next occurrence of the string " BIG ", you must call the
time. For an example of iterating over a search string to find all occurrences of the regular
expression, see
"Returning matched subexpressions" on page
and
REReplace
the string pattern that matches the regular expression with another string. You can use these
functions to replace the first match, or to replace all matches.
For detailed descriptions of the ColdFusion functions that use regular expressions, see CFML
Reference.
144
Chapter 7: Using Regular Expressions in Functions
and
REReplaceNoCase
use a regular expression to search a string for a pattern and return the
use regular expressions to search through a string and replace
REReplaceNoCase
and
Find
Replace
, for case-insensitive matching.
REFind
154.
functions, you
returns
Find
function a second
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