146
Chapter 7: Using Regular Expressions in Functions
Note:
The * only applies to the character set that immediately precedes it, not to the entire regular
expression.
A + after the closing square bracket specifies to find one or more occurrences of the character set.
You interpret the regular expression
"
[A-Z]+
"
as matching one or more uppercase letters
enclosed by spaces. Therefore, this regular expression matches " BIG " and also matches
“ LARGE ”, “ HUGE ”, “ ENORMOUS ”, and any other string of uppercase letters surrounded
by spaces.
Considerations when using special characters
Since a regular expression followed by an * can match zero instances of the regular expression, it
can also match the empty string. For example,
<cfoutput>REReplace("Hello","[T]*","7","ALL") -
#REReplace("Hello","[T]*","7","ALL")#<BR>
</cfoutput>
results in the following output:
REReplace("Hello","[T]*","7","ALL") - 7H7e7l7l7o
The regular expression [T]* can match empty strings. It first matches the empty string before “H”
in “Hello”. The “ALL” argument tells
REReplace
to replace all instances of an expression. The
empty string before “e” is matched and so on until the empty string before “o” is matched.
This result might be unexpected. The workarounds for these types of problems are specific to
each case. In some cases you can use [T]+, which requires at least one “T”, instead of [T]*.
Alternatively, you can specify an additional pattern after [T]*.
In the following examples the regular expression has a “W” at the end:
<cfoutput>REReplace("Hello World","[T]*W","7","ALL") –
#REReplace("Hello World","[T]*W","7","ALL")#<BR></cfoutput>
This expression results in the following more predictable output:
REReplace("Hello World","[T]*W","7","ALL") - Hello 7orld
Finding repeating characters
In some cases, you might want to find a repeating pattern of characters in a search string. For
example, the regular expression "a{2,4}" specifies to match two to four occurrences of “a”.
Therefore, it would match: "aa", "aaa", "aaaa", but not "a" or "aaaaa". In the following example,
the
REFind
function returns an index of 6:
<cfset IndexOfOccurrence=REFind("a{2,4}", "hahahaaahaaaahaaaaahhh")>
<!--- The value of IndexOfOccurrence is 6--->
The regular expression "[0-9]{3,}" specifies to match any integer number containing three or
more digits: “123”, “45678”, etc. However, this regular expression does not match a one-digit or
two-digit number.
You use the following syntax to find repeating characters:
•
{
m
,
n
}
Where
m
is 0 or greater and
n
is greater than or equal to
m
. Match
m
through
n
(inclusive)
occurrences.
The expression {0,1} is equivalent to the special character ?.
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: ......