简体   繁体   English

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

[英]Regex to find words between two tags

How can I use regex in python to find words between tags?如何在 python 中使用正则表达式来查找标签之间的单词?

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

You can use BeautifulSoup for this HTML parsing.您可以使用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()

Also, it's not a good practice to use str as a variable name in python as str() means a different thing in python.此外,在 python 中使用str作为变量名也不是一个好习惯,因为str()在 python 中意味着不同的东西。

By the way, the regex can be:顺便说一下,正则表达式可以是:

import re
print re.findall("<person>(.*?)</person>", input, re.DOTALL)
print re.findall("<location>(.*?)</location>", input, re.DOTALL)
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)

This example works for simple parsing only.此示例仅适用于简单解析。 Have a look at python official documentation on re看看关于re python 官方文档

To parse HTML, you should consider @sabuj-hassan answer but please remember to check this Stack Overflow gem as well.要解析 HTML,您应该考虑@sabuj-hassan 的答案,但请记住也检查此Stack Overflow gem

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>

We have a number of ways to import the data.我们有多种导入数据的方法。 Reading the file from disk:从磁盘读取文件:

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

Reading the data from a string:从字符串中读取数据:

root = ET.fromstring(country_data_as_string)

Other python Xml & Html parser其他 python Xml & Html 解析器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM