【问题标题】:Regex to find words between two tags正则表达式查找两个标签之间的单词
【发布时间】:2014-04-10 11:23:09
【问题描述】:

如何在 python 中使用正则表达式在标签之间查找单词?

s = """<person>John</person>went to<location>London</location>"""
......
.......
print 'person of name:' John
print 'location:' London 

【问题讨论】:

标签: python regex


【解决方案1】:

您可以使用BeautifulSoup 进行此 HTML 解析。

input = """"<person>John</person>went to<location>London</location>"""
soup = BeautifulSoup(input)
print soup.findAll("person")[0].renderContents()
print soup.findAll("location")[0].renderContents()

另外,在 python 中使用str 作为变量名不是一个好习惯,因为str() 在 python 中意味着不同的东西。

顺便说一下,正则表达式可以是:

import re
print re.findall("<person>(.*?)</person>", input, re.DOTALL)
print re.findall("<location>(.*?)</location>", input, re.DOTALL)

【讨论】:

  • @Blender 我不知道如何消除标签。你能帮我吗?
  • .string 就是您所需要的。此外,.find('person') 等价于 .findAll('person')[0]
  • 这不会找到任何标签之间的所有文本(当然,这个问题不清楚)
  • 如果不想匹配标签,请使用正向前瞻和后向:(?&lt;=&lt;person&gt;)(.*?)(?=&lt;/person&gt;)
【解决方案2】:
import re

# simple example
pattern = r"<person>(.*?)</person>"
string = "<person>My name is Jo</person>"
re.findall(pattern, string, flags=0)

# multiline string example
string = "<person>My name is:\n Jo</person>"
re.findall(pattern, string, flags=re.DOTALL)

此示例仅适用于简单解析。看看python official documentation on re

要解析 HTML,您应该考虑@sabuj-hassan answer,但请记住也要检查此Stack Overflow gem

【讨论】:

  • 不起作用:re.sub(br'&lt;center&gt;(.*?)&lt;/center&gt;', b' ', b'&lt;center&gt;a\n&lt;/center&gt;b', re.DOTALL)
  • 这不是我给出的代码示例,但我建议您将re.DOTALL 替换为flags=re.DOTALL。我刚刚尝试过,它仍然有效。希望能解决你的问题:)
【解决方案3】:
probably you are looking for **XML tree and elements**
XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. ET has two classes for this purpose - ElementTree represents the whole XML document as a tree, and Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level.

19.7.1.2. Parsing XML
We’ll be using the following XML document as the sample data for this section:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

我们有多种方式来导入数据。从磁盘读取文件:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

从字符串中读取数据:

root = ET.fromstring(country_data_as_string)

其他 python Xml & Html 解析器

https://wiki.python.org/moin/PythonXml http://docs.python.org/2/library/htmlparser.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    相关资源
    最近更新 更多