Basic Regular Expression Rules; Character Classes - MACROMEDIA COLDFUSION 5-DEVELOPING Develop Manual

Table of Contents

Advertisement

Basic Regular Expression Rules

Basic Regular Expression Rules

This section describes the basic rules for creating regular expressions (REs),
including single-character regular expressions.
The following are the basic rules for regular expressions:

Character classes

In ColdFusion regular expressions, you can specify a character using one of the
POSIX character classes. You enclose the character class name inside two square
brackets, as in this example:
REReplace ("Macromedia Web Site","[[:space:]]","*","ALL")
Special characters are:
Any character that is not a special character matches itself.
A backslash (\) followed by any special character matches the literal character
itself, that is, the backslash escapes the special character.
A period (.) matches any character, including newline. To match any character
except a newline, use [^#chr(13)##chr(10)#], which excludes the ASCII carriage
return and line feed codes. The corresponding escape codes are \r and \n.
A set of characters enclosed in brackets ([]) is a one-character RE that matches
any of the characters in that set. For example, "[akm]" matches an "a", "k", or
"m". A dash in a set of characters inside braces indicates a range of characters; for
example, [a-z] matches any lowercase letter.
If the first character of a character set is the caret (^), the RE matches any
character except those in the set. It does not match the empty string; for example:
[^akm] matches any character except "a", "k", or "m". The caret loses its special
meaning if it is not the first character of the set.
To make a regular expression case insensitive, substitute individual characters
with character sets; for example, [Nn][Ii][Cc][Kk].
If you want to include ] (closing square bracket) in square brackets it must be the
first character. Otherwise, it does not work even if you use \]. The following
example illustrates this:
<!--- Want to replace closing square bracket and all a's with * --->
<cfset strSearch = "[Test message]">
<!--- Next line does not work since ] is not the FIRST character
within [] --->
<cfset re = "[a\]]">
<cfoutput>REReplace(#strSearch#,#re#,"*","ALL") -
#REReplace(strSearch,re,"*","ALL")#<br>
Neither ']' nor 'a' was replaced because we searched for 'a'
followed by ']'<br>
</cfoutput>
<!--- Next line works since ] is the FIRST character within [] --->
<cfset re = "[]a]">
<cfoutput>REReplace(#strSearch#,#re#,"*","ALL") -
#REReplace(strSearch,re,"*","ALL")#<br>
Both 'a' and ']' were Replaced with *<br></cfoutput>
+ * ? . [ ^ $ ( ) { | \
261

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 5

Table of Contents