Regular Expression Quote Makes Finding Literals Easy In ColdFusion Search

<!---
	Store some text that we want to search. We are going
	to make sure that this text has characters that would
	be considered special characters within a regular
	expression.
--->
<cfsavecontent variable="strText">
	Hey Maria, you better stop. I don't think it's a good
	idea for you to change while I'm still in the room?!?!?
	I mean, sure you're looking hella fine [sic]! But,
	what would your parents think?!?!?
</cfsavecontent>
 
 
<!---
	We are going to store the search phrase in variable.
	This is just to demonstrate that the search phrase
	could come from anywhere, including a search form
	with user-entered criteria.
 
	In our case, we are going to use one phrase that has
	the ? which is a zero-or-more matcher and the []
	which creates a character set.
--->
<cfset strPhrase1 = "?!?!?" />
<cfset strPhrase2 = "[sic]" />
 
 
<!---
	Now, let's create a Java pattern to find our search
	phrase. Notice that we are putting the above search
	phrase into our patterns and using the \Q ... \E
	escape pattern. Using \Q and \E will match literal
	values in between even if they contain special
	regular expression characters.
--->
<cfset objPattern = CreateObject(
	"java",
	"java.util.regex.Pattern"
	).Compile(
		"(?i)(\Q#strPhrase1#\E|\Q#strPhrase2#\E)"
		)
	/>
 
<!---
	Create a matcher for out pattern that will be able
	to search the target string for out literal pattern.
--->
<cfset objMatcher = objPattern.Matcher( strText ) />
 
 
<!---
	Keep looping over the matcher until we have run out
	of matching patterns.
--->
<cfloop condition="objMatcher.Find()">
 
	<p>
		Found: #objMatcher.Group()#<br />
		Found At: #objMatcher.Start()#
	</p>
 
</cfloop>

For Cut-and-Paste