使用Python中的Regex从特定的xml标记中提取特定值

时间:2018-02-17 00:10:41

标签: python regex

我的xml:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

我需要一个正则表达式来提取“这个周末不要忘记我!”在标记中以及使用正则表达式在python中存在标记。

我编写了一段代码,但我无法弄清楚正则表达式。

1 个答案:

答案 0 :(得分:1)

基本解决方案:

import re  


data  = """
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>"""

found = re.findall('<body>(.*)</body>', data)

if found:
  for x in found:
    print(x)

>> Don't forget me this weekend!