Search code examples
regexcoldfusioncoldfusion-8

Coldfusion Regex get strings between tags


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


Solution

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