House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Search cf-talk

July 04, 2009

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31   

Home /  Groups /  ColdFusion Talk (CF-Talk)

Calling a function on a regex backreference - SOLVED

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
no problem:
Ryan Mitchell
11/02/06 09:22 A
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Ryan Mitchell
11/02/2006 08:24 AM

wonderful... the java solution worked a treat! thanks!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Rob Wilkerson
11/02/2006 08:38 AM

> wonderful... the java solution worked a treat! thanks! Would you mind posting the final code?  I'd like to see how that operates. Thanks.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Ryan Mitchell
11/02/2006 09:22 AM

no problem: jrereplace(s,"(mailto:)([\w.]+@[\w.]+)",helper,'ALL'); the helper is a reference to a function, which takes 2 params, 1 is the full matched string, 2 is an array of the portions to the matched string: function helper(s,g) { return obfuscate(s); } the jrereplace is defined as: <cffunction name="JREReplace" access="public" returntype="string" output="false" hint="This performs Java REReplaces on a string."> <!--- Define arguments. ---> <cfargument name="Text" type="string" required="true" /> <cfargument name="Pattern" type="string" required="true" /> <cfargument name="Target" type="any" required="true" /> <cfargument name="Scope" type="string" required="false" default="ONE" /> <cfscript> // Define the local scope. var LOCAL = StructNew(); // Check to see if we are using a string replace or a method // helper replace. if (IsSimpleValue( ARGUMENTS.Target )){ // We are doing a standard string replace, so just // use Java's string replacement. Check the scope. if (NOT Compare( ARGUMENTS.Scope, "ALL" )){ // Replace all. return( CreateObject( "java", "java.lang.String" ).Init( ARGUMENTS.Text ).ReplaceAll( ARGUMENTS.Pattern, ARGUMENTS.Target ) ); } else { // Replace one. return( CreateObject( "java", "java.lang.String" ).Init( ARGUMENTS.Text ).ReplaceFirst( ARGUMENTS.Pattern, ARGUMENTS.Target ) ); } } else { // We are using a function here to replace out the // groups. That means that matches have to be // evaluated and replaced on an individual basis. // Create the java pattern complied to the given regular // expression. LOCAL.Pattern = CreateObject( "java", "java.util.regex.Pattern" ).Compile( ARGUMENTS.Pattern ); // Create the java matcher based on the given text using the // compiled regular expression. LOCAL.Matcher = LOCAL.Pattern.Matcher( ARGUMENTS.Text ); // Create a string buffer to hold the results. LOCAL.Results = CreateObject( "java", "java.lang.StringBuffer" ).Init(); // Loop over the matcher while we still have matches. while ( LOCAL.Matcher.Find() ){ // We are going to build an array of matches. LOCAL.Groups = ArrayNew( 1 ); for ( LOCAL.GroupIndex = 1 ; LOCAL.GroupIndex LTE LOCAL.Matcher.GroupCount() ; LOCAL.GroupIndex = (LOCAL.GroupIndex + 1) ){ // Add the current group to the array of groups. ArrayAppend( LOCAL.Groups, LOCAL.Matcher.Group( JavaCast( "int", LOCAL.GroupIndex ) ) ); } // Replace the current match. Be sure to get the value by // using the helper function. LOCAL.Matcher.AppendReplacement( LOCAL.Results, // Call the target function pointer using function notation. ARGUMENTS.Target( LOCAL.Matcher.Group(), LOCAL.Groups ) ); // Check to see if we need to break out of this. if (NOT Compare( ARGUMENTS.Scope, "ONE" )){ break; } } // Add what ever is left of the text. LOCAL.Matcher.AppendTail( LOCAL.Results ); // Return the string buffer. return( LOCAL.Results.ToString() ); } </cfscript> </cffunction>


<< Previous Thread Today's Threads Next Thread >>

Mailing Lists