在两个标签之间查找单词的正则表达式

8
我该如何在Python中使用正则表达式来查找标签之间的单词?
s = """<person>John</person>went to<location>London</location>"""
......
.......
print 'person of name:' John
print 'location:' London 

2
最好使用像BeautifulSoup这样的HTML/XML解析器。 - Jayanth Koushik
在任何标签之间还是只是使用“个人”和“位置”标签? - dorvak
2
请查看著名的https://dev59.com/X3I-5IYBdhLWcg3wq6do相关问题。 - isedev
3个回答

13
你可以使用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 我不知道如何去除标签。你能帮我吗? - Sabuj Hassan
1
.string 就是你所需要的。此外,.find('person') 等同于 .findAll('person')[0] - Blender
这不会找到任何标签之间的所有文本(当然,问题对此并不清楚)。 - dorvak
如果您不想匹配标签,请使用正向预查和反向预查:(?<=<person>)(.*?)(?=</person>) - Paul Trimor

10
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官方文档关于re的内容。

如果要解析HTML,建议您考虑@sabuj-hassan的回答,但请务必记得同样查看这个Stack Overflow gem


无法工作:re.sub(br'<center>(.*?)</center>', b' ', b'<center>a\n</center>b', re.DOTALL) - e-info128
这并不是我给出的代码示例,但我建议您将 re.DOTALL 替换为 flags=re.DOTALL。我刚试过了,它仍然有效。希望能解决您的问题 :) - abrunet

1
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
Well 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接