The Portable Operating Systems Interface (POSIX) standard defines character combinations. These combinations are available to ColdFusion developers for use within regular expressions. They help to make regular expressions easier to write, read, and maintain. Different POSIX character classes are available to ColdFusion. The most common ones include the following:
Other available POSIX character classes that you can use within your ColdFusion code include the following:
One rule to remember when using POSIX character classes is that they must always be contained within two pairs of square brackets. Let's start with a simple example: <cfset secretmessage = REReplace("E!V!A!C!U!A!T!E! !N!O!W!", "[[:punct:]]", "", "ALL")> The preceding regular expression returns "EVACUATE NOW" when output. The [:punct:] POSIX character class matches all the ! characters in the string and replaces them with the specified string. In this case, "", which is an empty string. Now let's look at something a little more complex: <cfset secretmessage = REReplace(REReplace("I243*w22i3423ll*m2e678e21t234*y234121ou231*083l3452a2343te34r", "[[:digit:]]", "", "ALL"), "[[:punct:]]", " ", "ALL")> Remember that ColdFusion functions enable recursive processing. The preceding example shows the REReplace() regular expression function calling itself, but each call replaces different character types. The resulting string after all iterations of the REReplace() function is "I will meet you later". It's pretty easy to do what we've done in the preceding examples. We know the strings and know what needs to be replaced. However, what if we don't have that information? Let's look at some functions that can help us with that problem. |