
Multicharacter Regular Expressions
263
Multicharacter Regular Expressions
You can use the following rules to build multicharacter regular expressions:
•
Parentheses group parts of regular expressions together into grouped
subexpressions that you can be treat as a single unit; for example, (ha)+ matches
one or more instances of “ha”.
•
A plus sign (+) following a one-character regular expression or grouped
subexpressions matches one or more occurrences of the regular expression; for
example, [a-z]+ matches one or more lowercase characters.
•
An asterisk (*) following a one-character regular expression or grouped
subexpressions matches zero or more occurrences of the regular expression; for
example, [a-z]* matches zero or more lowercase characters. Since a regular
expression followed by an * can match the empty string, you can get unexpected
results when there is no actual match. 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
Here the regular expression [T]* can match empty strings. It first matches the
empty string before “H” in “Hello”. Next, (note that the “ALL” artgument 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 might be able to specify an additional pattern
after [T]*. In the following example 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
•
A one-character regular expression or grouped subexpression followed by a
question mark (?) matches zero or one occurrences of the regular expression; for
example, xy?z matches either “xyz” or “xz”.
•
The concatenation of regular expressions creates a regular expression that
matches the corresponding concatenation of strings; for example, [A-Z][a-z]*
matches any capitalized word.
•
The OR character (|) allows a choice between two regular expressions; for
example, jell(y|ies) matches either “jelly” or “jellies”.
•
The following suffixes match repetitions of a regular expresion:
−
{m,n}, where m is 0 or greater and n is greater than or equal to m, forces a
match of m through n (inclusive) occurrences of the preceding regular
expression; for example, (ba){2,4} matches “baba”, “bababa”, and
“babababa”, but not “ba” or “babababababa”.
−
{m,} forces a match of at least m occurrences of the preceding regular
expression. The syntax {,n} is not allowed.
Содержание COLDFUSION 5-DEVELOPING
Страница 1: ...Macromedia Incorporated Developing ColdFusion Applications MacroMedia ColdFusion 5 ...
Страница 58: ...38 Chapter 3 Querying a Database ...
Страница 134: ...114 Chapter 7 Updating Your Database ...
Страница 210: ...190 Chapter 10 Reusing Code ...
Страница 232: ...212 Chapter 11 Preventing and Handling Errors ...
Страница 238: ...218 Chapter 12 Using the Application Framework ...
Страница 262: ...242 Chapter 12 Using the Application Framework ...
Страница 278: ...258 Chapter 13 Extending ColdFusion Pages with CFML Scripting ...
Страница 320: ...300 Chapter 15 Indexing and Searching Data ...
Страница 336: ...316 Chapter 16 Sending and Receiving E mail ...
Страница 374: ...354 Chapter 18 Interacting with Remote Servers ...