Downloadable Files
jregetnocase_function.cfm.txt ( 3,755 Bytes )
This uses a case-insensitive Java regular expression to get the matching groups found in the given string. Java's regular expressions are much faster and more powerful than ColdFusions and so this method can be much more useful than any buit in ColdFusion tag; however, realize that under the hood, this method is creating several Java object and parsing out a string, so there will be some overhead associated with it.
Please view the method definition below or download the demonstration code.
- <cffunction name="JREGetNoCase" access="public" returntype="array" output="no"
- hint="This takes a string and returns an array of all the matches using the case insensitive Java RegExp.">
- <!--- Define arguments. --->
- <cfargument name="Text" type="string" required="true" />
- <cfargument name="RegExp" type="string" required="true" />
- <cfscript>
- // Define the local scope.
- var LOCAL = StructNew();
- // Set up flags for the Java pattern class.
- LOCAL.Flags = StructNew();
- LOCAL.Flags.CANON_EQ = 128;
- LOCAL.Flags.CASE_INSENSITIVE = 2;
- LOCAL.Flags.COMMENTS = 4;
- LOCAL.Flags.DOTALL = 32;
- LOCAL.Flags.MULTILINE = 8;
- LOCAL.Flags.UNICODE_CASE = 64;
- LOCAL.Flags.UNIX_LINES = 1;
- // Create the java pattern complied to the given regular expression.
- // When compiling, set it to match on multiple lines and do a case
- // insensitive search.
- LOCAL.Pattern = CreateObject("java", "java.util.regex.Pattern").Compile(
- ARGUMENTS.RegExp,
- BitOR(
- LOCAL.Flags.CASE_INSENSITIVE,
- LOCAL.Flags.MULTILINE
- )
- );
- // Create the java matcher based on the given text using the
- // compiled regular expression.
- LOCAL.Matcher = LOCAL.Pattern.Matcher( ARGUMENTS.Text );
- // Create an array to capture the matches.
- LOCAL.Matches = ArrayNew( 1 );
- // Loop over the matcher while we still have matches.
- while ( LOCAL.Matcher.Find() ){
- // Get the current match.
- ArrayAppend( LOCAL.Matches, LOCAL.Matcher.Group() );
- }
- // Return the results array which may or may not have any matches in it.
- return( LOCAL.Matches);
- </cfscript>
- </cffunction>
Added April 28, 2006 /
Updated April 28, 2006