python - Reading regular expressions from a XML file, store them into a list of list and afterwards use them -
i want read many regular expressions xml file, store them list of list , use them. solution not work , not know why.
suppose have xml contains regex want store in list of list. xml this:
<?xml version="1.0" encoding="iso-8859-1"?> <my_xml>     <field>         <regex>\d+\.\d+</regex>     </field>      <field>         <regex>\d+</regex>     </field> </my_xml> so, read line line xml file , build list of list containing regex:
tree = et.parse("./my_file.xml") root = tree.getroot() listoflist = []  field in root.findall('field'):      tmp = []     regex = str(field.find('regex').text)     tmp.append(regex)     listoflist.append(tmp) now, list of list contains regex ready. in fact, if print listoflist get:
[['\\d+\\.\\d+'], ['\\d+']] now, it's time use list of list contains 2 regex. suppose have string contains ver=4.0 , want 4.0. that, use regex contained listoflist[0]. here code:
 my_str = "ver=4.0"  print re.findall(str(listoflist[0]), my_str) .....but if that, python prints out ['.'] .
why python print out ['.'] instead of 4.0? how solve problem?
thanks.
the regex using
str(listoflist[0]) will in example be
["r'\\d+\\.\\d+'"] and literally ["r' before , '"] after.
the str functions formats array string representation of array.
you want actual regex not string representation.
use
re.findall(listoflist[0][0], my_str) because listoflist[0][0] \d+\.\d+ wich regex need.
Comments
Post a Comment