Input Considerations

Because of a peculiarity in ColdFusion’s implementation of regular expressions, certain regular expressions may cause a “Bad regular expression” error when applied to certain strings longer than 20,000 characters. If your application deals with strings longer than 20,000 characters, you should break the string into several smaller pieces, each 20,000 characters or less to avoid the possibility of encountering this behavior. In cases where you don’t know or can’t limit the maximum string length ahead of time, this is especially important. Example 17-1 shows one way to handle this dynamically, regardless of the string length.

Example 17-1. Dynamically Handling Large Strings

<CFSET Cutoff = 20000> <CFSET Start = 1> <CFSET Output = ""> <CFLOCK NAME="FileLock" TYPE="ReadOnly" TIMEOUT="30"> <CFFILE ACTION="Read" FILE="c:\test_in.txt" VARIABLE="Input"> </CFLOCK> <!--- if the string is greater than the cutoff length, break it up and apply the regular expression to each chunk. Then, create a new string by combining all of the chunks. ---> <CFIF Len(Input) GT Cutoff> <CFLOOP INDEX="i" FROM="1" TO="#Ceiling(Len(Input)/Cutoff)#"> <CFSET Finish = (Cutoff + Start)-1> <CFIF Finish GT Len(Input)> <CFSET Cutoff = (Len(Input)-Start)+1> </CFIF> <CFSET temp = Mid(Input, Start, Cutoff)> <CFSET "Part#i#" = ReReplace(temp, "<[^>]*>", "", "All")> <CFSET Output = Output & Evaluate("Part#i#")> <CFSET Start = Finish+1> </CFLOOP> <CFELSE> <CFSET Output = ReReplaceNoCase(Input, "<[^>]*>", ...

Get Programming ColdFusion now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.