I am trying to do a regex to just get the error code from this XML.
>>> re_code = re.compile(r'<errorcode>([0-9]+)</errorcode>', re.MULTILINE)
>>> re_code.match('''<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
... <methoderesponse>
... <status>
... <message/>
... <errorcode>515</errorcode>
... <value>ERROR</value>
... </status>
... </methoderesponse>
... ''')
It should be quite easy. But I don’t understand why it doesn’t match.
as @Jon Clements has said,
.match()only works if the expression is supposed to run from the beginning of the string,.search()searches the string for the first occurrence, and.findall()searches for all the occurrences.but regardless of that, you should modify slightly your regular expression to a slightly more readable version:
you don’t need the
re.MULTILINEargument, it does not pertain to this problem.