Finding Repeating Characters - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

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
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
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
function returns an index of 6:
REFind
<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 ?.
146
Chapter 7: Using Regular Expressions in Functions
[A-Z]+
as matching one or more uppercase letters
"
"
to replace all instances of an expression. The
REReplace

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion mx

Table of Contents