Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式查找两个标记之间的单词_Python_Regex - Fatal编程技术网

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

Python 正则表达式查找两个标记之间的单词,python,regex,Python,Regex,如何在python中使用正则表达式在标记之间查找单词 s = """<person>John</person>went to<location>London</location>""" ...... ....... print 'person of name:' John print 'location:' London s=“”约翰温特·托隆顿“” ...... ....... 打印“姓名的人:”John 打印“地点:”伦敦 您可以使用Bea

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

s = """<person>John</person>went to<location>London</location>"""
......
.......
print 'person of name:' John
print 'location:' London 
s=“”约翰温特·托隆顿“”
......
.......
打印“姓名的人:”John
打印“地点:”伦敦

您可以使用
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()
重新导入
#简单例子
模式=r“(.*)”
string=“我叫乔”
关于findall(模式、字符串、标志=0)
#多行字符串示例
string=“我的名字是:\n Jo”
re.findall(模式、字符串、标志=re.DOTALL)
此示例仅适用于简单解析。看看

解析HTML时,您应该考虑,但请记住也检查一下。

从字符串读取数据:

root = ET.fromstring(country_data_as_string)
其他pythonxml和Html解析器


最好在任何标记之间或仅在person和location标记之间使用类似BeautifulSoup的html/xml解析器?请参阅著名的@Blender,我不知道如何消除标记。你能帮我吗?
。string
就是你所需要的。另外,
.find('person')
相当于
.findAll('person')[0]
。这不会在任何标记之间找到所有文本(当然,这个问题不清楚)。如果您不想匹配标记,请使用正向的前向和后向:
(?不起作用:
re.sub(br'(*?),b'a\nb',re.DOTALL)
这与我给出的代码示例不完全相同,但我建议您将
re.DOTALL
替换为
flags=re.DOTALL
。我刚刚尝试过,但仍然有效。希望解决您的问题:)
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)