ColdFusion - regex callback for function call

I am using the ColdFusion reReplace () function to replace regular expressions.

I would like to use a function call for the replacement string and pass it the corresponding link.

Something like that:

<cfset s = "STARTDATE_2010-05-07 00:05:00.0_ENDDATE" />
<cfset s = reReplace(s, "STARTDATE_([\s-.:0-9]*)_ENDDATE", dateAdd("h", 1, "\1")) />

But this fails because "the value of parameter 3, which is currently \ 1, must be the value of the java.util.Date class."

Is there any other way to achieve this?

Thanks Stu

+3
source share
3 answers

I'm not too sure that what you want to do is possible using the approach described in your example, but here is a diagram of another approach that should work.

  • Separate date string
  • CreateDateTime
  • , DateAdd
  • DateFormat

ColdFusion , /, / , /. , ColdFusion .

+2

, , .

, "" , .

, Match Groups - , , , .

:

<cfset Jrex = createObject('component','jre-utils').init() />


<cfset MyString = "STARTDATE_2010-05-07 00:05:00.0_ENDDATE" />
<cfset MyRegex = "STARTDATE_([\s-.:0-9]*)_ENDDATE" />
<cfset MyString = Jrex.replace( MyString , MyRegex , addHour , 'all' )/>


<cffunction name="addHour" returntype="String" output="false">
    <cfargument name="Match"  type="String"/>
    <cfargument name="Groups" type="Array" default="#ArrayNew(1)#"/>

    <cfset var Result = DateAdd('h',1,Groups[1]) />

    <cfreturn DateFormat( Result , 'yyyy-mm-dd' )
        & ' ' & TimeFormat( Result , 'HH:mm:ss' )
        />
</cffunction>


: http://www.hybridchill.com/projects/jre-utils.html

, java.util.regex, CF org.apache.oro.text.regex engine, , ().

, , , .

, , , , , : , - :

Jrex.replace( MyString , MyRegex , "\F:DateAdd('h',1,\1)" , 'all' )

, - .

+1

, , . reReplace, replace() replaceNoCase().

<cfset s = "STARTDATE_2010-05-07 00:05:00.0_ENDDATE" />
<cfset s = replaceNoCase(replaceNoCase(s, "STARTDATE_", ""),"_ENDDATE","")>
<cfoutput>
    <cfif isDate(s)>
        before: #s# after: #dateAdd("h", 1, s)#
    </cfif> 
</cfoutput>

dateAdd () requires a valid date as a parameter, I don’t think you can do it with regex backreference, which will always be a string.

0
source

Source: https://habr.com/ru/post/1744318/


All Articles