Regular expression syntax
145
Basic regular expression syntax
The simplest regular expression contains only a literal characters. The literal characters must
match exactly the text being searched. For example, you can use the regular expression function
REFind
to find the string pattern " BIG ", just as you can with the
Find
function:
<cfset IndexOfOccurrence=
REFind
("
BIG
", "Some BIG string")>
<!--- The value of IndexOfOccurrence is 5 --->
In this example,
REFind
must match the exact string pattern " BIG ".
To use the full power of regular expressions, combine literal characters with character sets and
special characters, as in the following example:
<cfset IndexOfOccurrence=REFind("
[A-Z]+
", "Some BIG string")>
<!--- The value of IndexOfOccurrence is 5 --->
The literal characters of the regular expression consists of the space characters at the beginning
and end of the regular expression. The character set consists of that part of the regular expression
in square brackets. This character set specifies to find a single uppercase letter from A to Z,
inclusive. The plus sign (+) after the square brackets is a special character specifying to find one or
more occurrences of the character set.
If you removed the + from the regular expression in the previous example, " [A-Z] " matches a
literal space, followed by any single uppercase letter, followed by a single space. This regular
expression matches " B " but not " BIG ". The
REFind
function returns 0 for the regular
expression, meaning that it did not find a match.
You can construct very complicated regular expressions containing literal characters, character
sets, and special characters. Like any programming language, the more you work with regular
expressions, the more you can accomplish with them. The examples in this section are fairly basic.
For more examples, see
“Regular expression examples” on page 158
.
Regular expression syntax
This section describes the basic rules for creating regular expressions.
Using character sets
The pattern within the square brackets of a regular expression defines a character set that is used
to match a single character. For example, the regular expression " [A-Za-z] " specifies to match
any single uppercase or lowercase letter enclosed by spaces. In the character set, a hyphen indicates
a range of characters.
The regular expression " B[IAU]G " matches the strings “ BIG “, “ BAG “, and “ BUG “, but does
not match the string " BOG ".
If you specified the regular expression as " B[IA][GN] ", the concatenation of character sets
creates a regular expression that matches the corresponding concatenation of characters in the
search string. This regular expression matches a space, followed by “B”, followed by an “I” or “A”,
followed by a “G” or “N”, followed by a trailing space. The regular expression matches “ BIG ”, “
BAG ”, “BIN ”, and “BAN ”.
The regular expression [A-Z][a-z]* matches any word that starts with an uppercase letter and is
followed by zero or more lowercase letters. The special character * after the closing square bracket
specifies to match zero or more occurrences of the character set.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......