roger v
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?
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>
Sorry.. missed a parenthesis in the second expression..
MyErrorCode = ReFind(MyXmlFile, "(?<=<error code=\")\d+(?=\">)")
MyErrorText = ReFind(MyXmlFile, "(?<=<error code=\"\d+\">)[^<]+(?=</error>)" )
ASKER
@mqh mqharish:
I get an invalid token '?' found error with coldfusion 7
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.
(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>" )
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#">
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_x xxx.cfm: line 443
441 :
442 : <cfset testFile = xmlParse(arrWmObjectSucces s[3].POSTR ESULT.File content)>
443 : <cfset MyErrorCode = ReFind(testFile, "<error code=\"\d+\">")>
444 : <cfset MyErrorText = ReFind(testFile, "<error code=\"\d+\">[^<]+</error> " )>
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
441 :
442 : <cfset testFile = xmlParse(arrWmObjectSucces
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:
Try this:
MyErrorCode = ReFind("(?<=<error code=\")\d+(?=\">)", MyXmlFile)
MyErrorText = ReFind("(?<=<error code=\"\d+\">)[^<]+(?=</error>)" , MyXmlFile)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@mq
Now it gives the invalid token "?" error! :)
Now it gives the invalid token "?" error! :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.l
ColdFusion was looking at the following text:
\\
?? Where's \\ ??
\\
?? Where's \\ ??
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..
I checked to make sure there are no other slashes. I'm guessing it's getting thrown off by the \ inside the pattern..
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
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
Open in new window