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

Mailing Lists
Home / Groups / ColdFusion Talk (CF-Talk)

REReplaceNoCase and dynamic query columns

Author:
Peter Boughton
02/02/2010 05:39 AM

>Is it possible to dynamically evaluate the temp variable from the >regular expression and use it as a ColdFusion variable? It's not a "temp variable from the regular expression", it is a variable which exists *within the scope* of that regular expression. That's important to understand - given this expression: REReplaceNoCase(REQUEST.theURL, '%%(\w+)%%', "#REQUEST.myQ[/\1][1]#", 'ALL') The CF expression is evaluated *before* the regex does its stuff, and thus the \1 never enters the regex 'scope', and doesn't gets treated as a regex back-reference. (hope that explanation makes sense?) Depending on if/how you want to handle invalid columns, one solution which wont cause errors is to simply loop through the query column names, like so: <cfset Request.Final = REQUEST.theURL /> <cfloop index="CurCol" list="#REQUEST.myQ.ColumnList#">     <cfset Request.Final = replace(Request.Final,'%%#CurCol#%%',REQUEST.myQ[CurCol][1],'all') /> </cfloop> That will mean that any mis-named columns are still in the string - so you may want/need to look for remaining %% in the string and behave appropriately. But, going back to the regex solution for this - which can be more flexible, and is definitely worth knowing - the way to do this is to use a regex replace function which allows you to pass a function as the third argument. Afaik none of the three main CFML engines allow this (yet), but I do have a CFC that enables assorted regex features, including passing a function: http://hybridchill.com/projects/jre-utils.html Once you've created the object (named jre below), you can then do: <cfset REQUEST.final = jre.replace( REQUEST.theURL , '%%(\w+)%%' , MyFunction , 'all' ) /> Then, you can create a function (see the "callback function" example at the above link) named MyFunction and return this: <cfreturn REQUEST.myQ[ Arguments.Groups[1] ][1] /> And that should all ultimately work as you were going for initially. Hopefully all this makes sense - let me know if any questions/etc. :)


Search cf-talk

February 08, 2012

<<   <   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