我有一堆需要在字符串中找到的模式,它们如下:<dyn type="dataFrame" name="Main Map" property="reference scale"/><dyn type="dataFrame" name="Main Map" property="time"/><dyn type="page" property="name"/><dyn type="page" property="number"/><dyn type="page" property="index"/><dyn type="page" property="count"/><dyn type="page" property="attribute" field="<Field Name>" domainlookup="true"/><dyn type="page" property="attribute" field="<Field Name>" />用法示例:Page <dyn type="page" property="index"/> of <dyn type="page" property="count"/>这将导致Page 1 of 15我计划使用以下正则表达式:<dyn[^>]*/>这将给:regex = re.compile("<dyn[^>]*/>")string = """Page <dyn type="page" property="index"/> of <dyn type="page" property="count"/>"""r = regex.search(string)print regex.findall(string)[u'<dyn type="page" property="index"/>', u'<dyn type="page" property="count"/>']但我不知道这是否是最佳模式(我相信有更好的方法)。这将找到具有该模式的所有模式,但不会找到标签内的属性。有没有一种写正则表达式的方法,可以将值以内的所有值作为键,而将=符号后的值压入字典对象?我只是认为有一种更好的方法来执行此操作,并且由于我不是正则表达式的高手,所以我想向社区询问。谢谢 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 (\S+)="([^"]+)"试试看,获取捕捉,请看演示。https://regex101.com/r/nL5yL3/42将group 1作为键,将group 2作为值。 (adsbygoogle = window.adsbygoogle || []).push({});
07-26 07:08