Coldfusion Regex find question

roger v
roger v used Ask the Experts™
on
I have an xml element in a struct. I need to find some values in that xml document.

My example code is attacehd. I need to find the value that is between the <leadId> and </leadId> element, as well as the value in the attribute of <error code="0">need to find this text</error>

So I need something like:
myLead = ReFind(myXMLFile, "regex pattern")
myErrorCode and myErrorText

How do I do this?

       
<?xml version="1.0" encoding="utf-8"?> <postResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xxxxx.com/xxxxxx/services/2008/09/indry.asmx">  <leadId>0</leadId>  <successful>false</successful>  <message>Error occurred, see errors for details</message>  <errors>  <error code="0">Invalid Postal Code (34343)</error>  </errors> </postResponse>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this:
MyErrorCode = ReFind(MyXmlFile, "(?<=<error code=\")\d+(?=\">)")
MyErrorText = ReFind(MyXmlFile, "(?<=<error code=\"\d+\">)[^<]+(?=</error>" )

Open in new window

Sorry.. missed a parenthesis in the second expression..

MyErrorCode = ReFind(MyXmlFile, "(?<=<error code=\")\d+(?=\">)")
MyErrorText = ReFind(MyXmlFile, "(?<=<error code=\"\d+\">)[^<]+(?=</error>)" )

Open in new window

Author

Commented:
@mqh mqharish:

I get an invalid token '?' found error with coldfusion 7
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Oh ok.. Forgot that CF doesn't support lookbehinds.. Try this:

(Note that it would return the tags also, which you can later replace with empty string, to get only the value.

MyErrorCode = ReFind(MyXmlFile, "<error code=\"\d+\">")
MyErrorText = ReFind(MyXmlFile, "<error code=\"\d+\">[^<]+</error>" )

Open in new window


I think I would convert it to a structure using XML parse and then access the values directly in the structure.

<cfset theResult = xmlParse(MyXmlFile)>

<cfdump var="#theResult#">

Author

Commented:
I get this error with that:

 Missing argument name.
When using named parameters to a function, every parameter must have a name.

The CFML compiler was processing:

    * an expression beginning with "ReFind", on line 443, column 30.This message is usually caused by a problem in the expressions structure.
    * a cfset tag beginning on line 443, column 10.
    * a cfset tag beginning on line 443, column 10.

 
The error occurred in C:\Program Files\Apache Group\Apache2\home\xxx\xxx\xxx\xxx_xxxx.cfm: line 443

441 :       
442 :       <cfset testFile = xmlParse(arrWmObjectSuccess[3].POSTRESULT.Filecontent)>
443 :       <cfset MyErrorCode = ReFind(testFile, "<error code=\"\d+\">")>
444 :       <cfset MyErrorText = ReFind(testFile, "<error code=\"\d+\">[^<]+</error>" )>
LOL!! The parameters have to be in the other way around:.. I copy-pasted from your initial code ;-)

Try this:

MyErrorCode = ReFind("(?<=<error code=\")\d+(?=\">)", MyXmlFile)
MyErrorText = ReFind("(?<=<error code=\"\d+\">)[^<]+(?=</error>)" , MyXmlFile)

Open in new window

If you're referring to my suggestion of using XML, it would not be combined with a Find at all.

Once you have it parsed as XML, it would then exist in a structure, you can see the structure by the CFDUMP command, then it is easy to directly access the item such as...

<cfset theResult = xmlParse(MyXmlFile)>

#theResult.leadID#

But usually something a bit more than that.  In order to give you the exact variable, I would need to see the dump.   You should be able to look at the dump and follow the path names to the variable you want starting with TheResult...

Author

Commented:
@mq

Now it gives the invalid token "?" error!    :)
:D

I must be really sleepy... I copy pasted the wrong code (instead of my second comment, I copied the first)

MyErrorCode = ReFind("<error code=\"\d+\">", MyXmlFile)
MyErrorText = ReFind("<error code=\"\d+\">[^<]+</error>", MyXmlFile)

Open in new window

Author

Commented:
@gde:

Attached a screenshot of the cfdump. It is right below the Making SOAP call....
soapcall.doc

Author

Commented:
@mq:

Now this error:

 Invalid CFML construct found on line 448 at column 47.
ColdFusion was looking at the following text:

\\

The CFML compiler was processing:

    * an expression beginning with "\"", on line 448, column 29.This message is usually caused by a problem in the expressions structure.
    * an expression beginning with "ReFind", on line 448, column 22.This message is usually caused by a problem in the expressions structure.
    * a cfset tag beginning on line 448, column 2.
    * a cfset tag beginning on line 448, column 2.

Is your variable name postResponse?  If so...

#postResponse.leadID#

If not, add your variable name before the above..

#myVariable.postResponse.leadID#

ColdFusion was looking at the following text:

\\

?? Where's \\ ??

Author

Commented:
@mq:

I checked to make sure there are no other slashes. I'm guessing it's getting thrown off by the \ inside the pattern..

Author

Commented:
@gde,

Thanks much! I tried the same frigging thing before the posted the question but I guess I was doing something wrong cuz it kept blowing up. :D

roger

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial