Link to home
Start Free TrialLog in
Avatar of roger v
roger vFlag for United States of America

asked on

Coldfusion Regex find question

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

Avatar of Harisha M G
Harisha M G
Flag of India image

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

Avatar of roger v

ASKER

@mqh mqharish:

I get an invalid token '?' found error with coldfusion 7
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#">
Avatar of roger v

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of roger v

ASKER

@mq

Now it gives the invalid token "?" error!    :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of roger v

ASKER

@gde:

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

ASKER

@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 \\ ??
Avatar of roger v

ASKER

@mq:

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

ASKER

@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