Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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 如何从字符串中多次提取HTML标记模式?_Python_Html_Regex - Fatal编程技术网

Python 如何从字符串中多次提取HTML标记模式?

Python 如何从字符串中多次提取HTML标记模式?,python,html,regex,Python,Html,Regex,我有一个模式,我想根据这个模式搜索一个字符串以找到所有匹配项。使用findall()后,只打印最后一个匹配的 我要处理的字符串如下: 'LishuoBeijing CNLiuxin PengBeijing CNSunsheng Yanbeijing CNWanghua LittletonMauswang JunlittletonMaus' 我尝试使用以下代码从字符串中提取所有发明家 INVENTORS\u CONTENT\u PATTERN=re.compile(“(.*?”) 关于findal

我有一个模式,我想根据这个模式搜索一个字符串以找到所有匹配项。使用
findall()
后,只打印最后一个匹配的

我要处理的字符串如下:

'LishuoBeijing CNLiuxin PengBeijing CNSunsheng Yanbeijing CNWanghua LittletonMauswang JunlittletonMaus'

我尝试使用以下代码从字符串中提取所有发明家

INVENTORS\u CONTENT\u PATTERN=re.compile(“(.*?”)
关于findall(发明人、内容、模式、数据)
我得到的结果是最后一个匹配的结果,并非所有的发明家都来自以下数据:

['WangJunLittletonMAUS']

此表达式可能更接近您的想法:

<inventor sequence="[^"]*" designation="[^"]*">(.*?)<\/inventor>
(*)
试验
重新导入
regex=r'(*?)
测试_str=”“”
.如果你愿意,你可以
也可以观看,它将如何匹配
对照一些样本输入



此表达式可能更接近您的想法:

<inventor sequence="[^"]*" designation="[^"]*">(.*?)<\/inventor>
(*)
试验
重新导入
regex=r'(*?)
测试_str=”“”
. 如果你愿意,你可以
也可以观看,它将如何匹配
对照一些样本输入



designation=“.*”
中的通配符与文本的中间90%匹配。使用正则表达式解析嵌套结构(如HTML)会让人心碎。考虑使用一个专用的XML/HTML解析器。参见代码> >名称=“.*”< /代码>结束匹配文本的中间90%。使用ReGEX解析嵌套结构(如HTML)是心碎的秘诀。考虑使用专用的XML/HTML解析器。
['<addressbook><last-name>Li</last-name><first-name>Shuo</first-name><address><city>Beijing</city><country>CN</country></address></addressbook>', '<addressbook><last-name>Liu</last-name><first-name>Xin Peng</first-name><address><city>Beijing</city><country>CN</country></address></addressbook>', '<addressbook><last-name>Sun</last-name><first-name>Sheng Yan</first-name><address><city>Beijing</city><country>CN</country></address></addressbook>', '<addressbook><last-name>Wang</last-name><first-name>Hua</first-name><address><city>Littleton</city><state>MA</state><country>US</country></address></addressbook>', '<addressbook><last-name>Wang</last-name><first-name>Jun</first-name><address><city>Littleton</city><state>MA</state><country>US</country></address></addressbook>']