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 ?.
Содержание 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...