Is there a way to separate a string into multiple lines like so:
<cfset qSelect = "xxxxxxxxxxxxxxxx
bbbbbbbbbbbbbbbbbbbbbbbb
xxxxxxxxxxxxxxxxxxxx">
Sure, you can do
<cfset qSelect = "xxxxxxxxxxxxx" &
"bbbbbbbbbbbbbbbbb" &
"xxxxxxxxxxxxxxxxx" />
That what you mean?
As Adam suggested you can concatenate. Another choice would be cfsavecontent.
<cfsavecontent variable="qSelect">
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
</cfsavecontent>
this would capture the whole string with line breaks (if that's what you are after).
Related
I have a csv file composed from a header line (field list) and several detalis lines (details for each Customer).
This file contain a lot of unused fileds so I tried to rebuild a clean file in which I put only fields I need.
for that I loop over the header line, I get the index (position) of the field I need and stored it in a variable:
<cfset FirstName_Pos = listfind(header,'FirstName',';')>
<cfset LastName_Pos = listfind(header,'LastName',';')>
The separated header fields is the ';' character.
After that I retreive the positions of all needed fields, I create a new file to put in the desired info of each line
<cffile action="rename" source="#LocalPath#/#FileName#" destination="#LocalPath#/Source_#FileName#">
<cfset NewFile = FileOpen('#LocalPath#/#FileName#','Append')>
<cfset Newheader = 'FirstName,LastName'>
<cfset fileWriteLine(NewFile, Newheader)>
<cfloop file="#LocalPath#\Source_#FileName#" index="line">
<cfset count = count + 1>
<cfif count GTE 2>
<cfset FirstName= listgetat(line,FirstName_Pos,';',1)>
<cfset LastName= listgetat(line,LastName_Pos,';',1)>
<cfset detail = '#FirstName#,#LastName#'>
<cfset fileWriteLine(NewFile, detail)>
</cfif>
The problem is that in the details lines of the original file there are some fields written as follow :
"#08/04/14 23:00;08/05/14 23:00#"
i.e the field contains the ';' character which is my separated fields character I used in the listgetat function
Therefore, I get non desired value in the variable FirstName and LastName.
Considering that the original file contain the following info:
USERID;Post;FirstName;Date1;Mail;Date2;LastName;Telephone
123;Engineer;Alan;"#08/04/14 23:00;08/05/14 23:00#";alan#yahoo.fr;"#10/04/14 11:00;10/05/14 11:00#";Jones;0624262589
I get :
FirstName;LastName
Alan;"#10/04/14 11:00
instead of
FirstName;LastName
Alan;Jones
I get the idea to loop over all details line of the original file and replace the ';' charcater with a space or blank character using regular expression only on fields having the same format "#08/04/14 23:00;08/05/14 23:00#".
(The date change of course from one field to another and from one raw to another)
<cfloop file="#LocalPath#\Source_#FileName#" index="line">
<cfset newline = rereplace(line,'"##[^\w.];[^\w.]##"','"##[^\w.] [^\w.]##"','all')>
<cfset count = count + 1>
<cfif count GTE 2>
<cfset FirstName= listgetat(newline,FirstName_Pos,';',1)>
<cfset LastName= listgetat(newline,LastName_Pos,';',1)>
<cfset detail = '#FirstName#,#LastName#'>
<cfset fileWriteLine(NewFile, detail)>
</cfif>
</cfloop>
It doesn't work because it seems that the regular expression I used is completely wrong. And also maybe because I duplicate the # sign to deal with coldfusion syntax error
Can anyone has an idea about the regular expression I have to used to deal with this situation?
Thanks in advance
this is an example of an original file
USERID;Post;FirstName;Date1;Mail;Date2;LastName;Telephone
123;Engineer;Alan;"#08/04/14 23:00;08/05/14 23:00#";alan#yahoo.fr;"#10/04/14 11:00;10/05/14 11:00#";Jones;0624262589
parse the original file and replace all occurrences of 0;0 (number;number) with 0,0 (number,number). Then your original solution should work fine.
regex = "/d;/d" to track them down I believe.
I'm trying to teach myself ColdFusion.
I have a string coming in from the database in this format:
domain.com
<br/>
www.facebook.com/facebookpage
<br/>
http://instagram.com/instagrampage
It is all coming from #getRacer.txtDescription#. The format of this text will always be the same.
I need to split it into 3 variables. I tried this (derived from the example on the adobe website)
<h3>ListToArray Example</h3>
<cfset myList = ValueList(getRacer.txtDescription)>
<p>My list is a list with <cfoutput>#ListLen(myList)#</cfoutput> elements.
<cfset myArrayList = ListToArray(myList,'<br/>')>
<p>My array list is an array with
<cfoutput>#ArrayLen(myArrayList)#</cfoutput> elements.
I somehow ended up with 11 items in the array.
Thank you
This should work.
<cfset TestSTring = "domain.com<br/>www.facebook.com/facebookpage<br/>http://instagram.com/instagrampage">
<cfset a = TestString.Split("<br/>")>
The reason ListtoArray is displaying 11 items is because ColdFusion treats each character in the delimiter string (<br/>) as a separate delimiter
Based on #Leigh's comment updating my answer to ensure people should learn the Coldfusion APIs rather than fiddling with Java functions, <cfset a = ListToArray(TestString, "<br/>", false, true)> will also work. Thanks Leigh.
Note: The false at the end is for the includeEmptyFields flag and the true is for the multiCharacterDelimiter flag. See the docs.
<cfset myList = ReplaceNoCase(getRacer.txtDescription,'<br/>','|','ALL')>
<cfset myArrayList = ListToArray(myList,'|')>
I chose a pipe character because it is unlikely to already exist in your string. If you wanted to account for the possibility that your BR tag may or may not use XML syntax then you could you regex:
<cfset myList = ReReplaceNoCase(str,'<br/?>','|','ALL')>
<cfset myArrayList = ListToArray(myList,'|')>
We currently have some application variables being defined on our apps and also have a special set of tags that are processed within our templates. However I'm finding it to be a bit difficult to split the tags, so I can convert them to application variables when parsed by the template.
<cfset mystring = "[pss]'fetchMessages','VWO-Tracking-Code'[end_pss]">
<cfset the_match = REMatch("\[pss\]\s*(((?!\[pss\]|\[end_pss\]).)+)\s*\[end_pss\]",mystring) />
<cfdump var="#the_match#" />
Our goal is to split the strings between the "[pss] and [end_pss]"
The regex above simply takes the string and applies it to a CF array, which is all good. However I strictly want the code between it as I'll be able to convert it to
<cfset application.snippets['VWO-Tracking-Code']>
Right now it returns everything as one string and I also require the first portion to determine what type of functionality is required.
Any ideas on how to do this would be greatly appreciated.
We are currently using CF 8 and the structure of the code is exactly the same all the time. The only thing is it can be contained within other strings of code as well...
If you're only doing this for one set of tags you can use the replaceList function rather than regEx
<cfset mystring = "[pss]'fetchMessages','VWO-Tracking-Code'[end_pss]">
<cfset mystring = replaceList(mystring,"[pss],[end_pss],'",'')>
<cfset firstitem = listfirst(mystring)>
<cfset seconditem = listlast(mystring)>
<cfdump var="#firstitem#">
<cfdump var="#seconditem#">
This outputs fetchMessages and VWO-Tracking-Code. If you want/need the single quotes on your strings then you can remove the ,' from the 2nd parameter of the replaceList function.
I have an application that I have inherited that dynamically builds HREF links within the displayed text. All seemed to be working well until we recently did a database change and our list of terms to link started to get returned from the query in a different order. This exposed a bug within the existing REGEX where it tries to place an HREF link within a preexisting HREF. Simply forcing a new ordering on the terms list is not an option. A term could be just one word, could be multiple words and even could be words formatted using HTML.
What would I need to adjust within the REGEX so that it ignores terms within the HREF attribute of an A element? Here is an example of what I am referring to:
<cfset Output = "This is some sample text to show the problem when we have term1 term2." />
<cfloop index="w" list="sample;term1 term2;term1" delimiters=";">
<cfset Output = "." & Variables.Output & "." />
<cfset Output = REReplaceNoCase(Variables.Output, "(?![</]#w#>)(\W)(#w#)(\W)", "\1\2\3", "one") />
<cfset Output = Mid(Variables.Output, 2, Len(Variables.Output)-2) />
</cfloop>
<cfoutput>#Variables.Output#</cfoutput>
Change the first (\W) in the regex to ([^=\w>]) and the second to ([^=\w<])
<cfset Output = REReplaceNoCase(Variables.Output, "(?![</]#w#>)([^=\w>])(#w#)([^=\w<])", "\1\2\3", "one") />
I have HTML code as String variable in Coldfusion.
For example:
<cfset str = "<span class='xyz'> sample text within span </span>" >
Now I want to repalce the word span from text "sample text within span" but not the tag name.
Can someone help me?
Thanks in advance.
I'm going to dumbly answer your question:
<cfset str = "<span class='xyz'> sample text within span </span>" >
<cfdump var="#str#" />
<!--- Convert to list based on start and end tag brackets --->
<cfset arr = listToArray(str, ">,<") />
<!--- Replace the ACTUAL text --->
<cfset newStr = replace(str, arr[2], "my new text") />
<cfdump var="#newStr#" />
Disclaimer: if I caught myself writing this I would probably think myself on the wrong track.
This road leads to one trying to use regular expressions to parse HTML, regarded as a bad thing as mentioned in this article.
Perhaps you can explain your problem a little more and we can help.
I habe changed my code like this and it seems to work, but how efficent it is I don't know
Can someone please check it?
<cfset htmlcontents = ReReplaceNoCase(htmlcontents, "(>[^<]*?)(#Lcase(text2replace)#)", "\1<span class=suchehighlight>\2</span>","ALL")>
Thanks