stevenschulman
asked on
Displaying Session variables with multiple levels of indirection
I am trying to provide macro-like substitutions for strings from an input file. The input file might have the string "The user is: !user!."
Suppose Session.user = "Steve"
I want to output the string with !user! replaced with the value of "Session.user".
If I use:
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#Session.user#')>
<cfset line = "#line#">
I get "The user is: Steve." That is, this works, but "!xxxxxx!" would be replace with the value of Session.user.
If I use:
<cfset replaceString = "Session.\1">
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#replaceString#')>
<cfset line = "#line#">
<cfoutput>#line#</cfoutput >
I get "The user is: Session.user."
If I use:
<cfset replaceString = "Session.\1">
<cfset testReplace = "#replaceString#">
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#testReplace#')>
<cfset line = "#line#">
<cfoutput>#line#</cfoutput >
I get "The user is: Session.user."
If I use:
<cfset replaceString = "Session.\1">
<cfset testReplace = "###replaceString###">
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#testReplace#')>
<cfset line = "#line#">
<cfoutput>#line#</cfoutput >
I get "The user is: #Session.user#."
I need to have two evaluations. Thanks much for any help.
Suppose Session.user = "Steve"
I want to output the string with !user! replaced with the value of "Session.user".
If I use:
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#Session.user#')>
<cfset line = "#line#">
I get "The user is: Steve." That is, this works, but "!xxxxxx!" would be replace with the value of Session.user.
If I use:
<cfset replaceString = "Session.\1">
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#replaceString#')>
<cfset line = "#line#">
<cfoutput>#line#</cfoutput
I get "The user is: Session.user."
If I use:
<cfset replaceString = "Session.\1">
<cfset testReplace = "#replaceString#">
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#testReplace#')>
<cfset line = "#line#">
<cfoutput>#line#</cfoutput
I get "The user is: Session.user."
If I use:
<cfset replaceString = "Session.\1">
<cfset testReplace = "###replaceString###">
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#testReplace#')>
<cfset line = "#line#">
<cfoutput>#line#</cfoutput
I get "The user is: #Session.user#."
I need to have two evaluations. Thanks much for any help.
>>I want to output the string with !user! replaced with the value of "Session.user".
If I use:
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#Session.user#')>
<cfset line = "#line#">
I get "The user is: Steve." That is, this works, but "!xxxxxx!" would be replace with the value of Session.user.
isn't that what you want? or am i not reading the first line right?
what output are you looking to achieve?
If I use:
<cfset line = REreplaceNoCase(#line#, '!(.*)!', '#Session.user#')>
<cfset line = "#line#">
I get "The user is: Steve." That is, this works, but "!xxxxxx!" would be replace with the value of Session.user.
isn't that what you want? or am i not reading the first line right?
what output are you looking to achieve?
ASKER
!user! is only an example. If the text were "Hello !firstName! !lastName!" then !firstName! would be substituted with the value of Session.firstName and !lastName! would be substituted with the value of Session.lastName.
then you should not use a regex but a specific replace
<cfset temp=#replace(line,"!somes tring!", #session.somestring#,'all' )>
reg ex is for pattern matching you need exact matching
the pattern you are using, !(.*)!', will match !user! and !xxxxxx! but it would also match
this sentence ending an an exclamation! and the next sentence ending in an exclamation!
would look like this
this sentence ending an an exclamationSteve
so use exact match replacement
<cfset temp=#replace(line,"!somes
reg ex is for pattern matching you need exact matching
the pattern you are using, !(.*)!', will match !user! and !xxxxxx! but it would also match
this sentence ending an an exclamation! and the next sentence ending in an exclamation!
would look like this
this sentence ending an an exclamationSteve
so use exact match replacement
ASKER
I understand that, but with regular expressions whatever is enclosed in parenthesis is returned and reusable as \n. Thus, in this case, "!(.*)!" matches all characters between two exclamaition points and provides the specific values matched in "\1|. That is why I use "Session.\1". This does, in fact, work. I get the value "Session.user" or "Session.firstName" or "Session.lastName". The problem is that I have the string, but can not seem to go the next step to have it evaluated.
I tried using Evaluate() function. I suspect that the answer lies there somewhere, but I still can not get it to work.
I tried using Evaluate() function. I suspect that the answer lies there somewhere, but I still can not get it to work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window