
144
Chapter 7: Using Regular Expressions in Functions
About regular expressions
In traditional string matching, as used by the ColdFusion
Find
and
Replace
functions, you
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,
Find
returns
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
and
REReplaceNoCase
, for case-insensitive matching.
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
REFind
and
REFindNoCase
use a regular expression to search a string for a pattern and return the
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
REFind
function a second
time. For an example of iterating over a search string to find all occurrences of the regular
expression, see
“Returning matched subexpressions” on page 154
.
REReplace
and
REReplaceNoCase
use regular expressions to search through a string and replace
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
.
Содержание COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Страница 1: ...Developing ColdFusion MX Applications...
Страница 22: ...22 Contents...
Страница 38: ......
Страница 52: ...52 Chapter 2 Elements of CFML...
Страница 162: ......
Страница 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Страница 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Страница 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Страница 266: ......
Страница 314: ...314 Chapter 14 Handling Errors...
Страница 344: ...344 Chapter 15 Using Persistent Data and Locking...
Страница 349: ...About user security 349...
Страница 357: ...Security scenarios 357...
Страница 370: ...370 Chapter 16 Securing Applications...
Страница 388: ...388 Chapter 17 Developing Globalized Applications...
Страница 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Страница 410: ......
Страница 426: ...426 Chapter 19 Introduction to Databases and SQL...
Страница 476: ...476 Chapter 22 Using Query of Queries...
Страница 534: ...534 Chapter 24 Building a Search Interface...
Страница 556: ...556 Chapter 25 Using Verity Search Expressions...
Страница 558: ......
Страница 582: ...582 Chapter 26 Retrieving and Formatting Data...
Страница 668: ......
Страница 734: ...734 Chapter 32 Using Web Services...
Страница 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Страница 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Страница 788: ......
Страница 806: ...806 Chapter 35 Sending and Receiving E Mail...