【问题标题】:Extracting a specific value from a specific xml tag using Regex in Python [duplicate]在 Python 中使用正则表达式从特定 xml 标记中提取特定值 [重复]
【发布时间】:2018-07-27 22:39:30
【问题描述】:

我的 xml:

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

我需要一个正则表达式来提取“本周末不要忘记我!”在标签中,如果标签存在于 python 中,使用正则表达式。

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

【问题讨论】:

  • 我认为你应该看看正则表达式,然后对你拥有的正则表达式产生疑问,如果有的话
  • 我认为你应该包含你的代码。
  • 您确定正则表达式是正确的工具吗?这通常不是解析 xml 或 html 的正确方法。

标签: python regex


【解决方案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!

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 1970-01-01
    • 2014-05-30
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2013-04-16
    • 1970-01-01
    相关资源
    最近更新 更多