• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

17.6. 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">

<!--- 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>
    <CFSET temp = Mid(Input, Start, Cutoff)>
    <CFSET "Part#i#" = ReReplace(temp, "<[^>]*>", "", "All")>
    <CFSET Output = Output & Evaluate("Part#i#")>
    <CFSET Start = Finish+1>
  <CFSET Output = ReReplaceNoCase(Input, "<[^>]*>", "", "All")>

<CFLOCK NAME="FileLock" TYPE="Exclusive" TIMEOUT="30">
  <CFFILE ACTION="WRITE" FILE="c:\test_out.txt" OUTPUT="#Output#" ADDNEWLINE="No"> 





Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint