Python-re-正则表达式需要帮助吗
我自己搜索过,但无法成功生成正则表达式Python-re-正则表达式需要帮助吗,python,regex,Python,Regex,我自己搜索过,但无法成功生成正则表达式 html = ''' ...some htmls... <div id='client_info'> <p><b>[client_name]</b><br/><b>[client_company]</b></p> <p>[client_address]<br/>[client_CP]<br/>[client_c
html = '''
...some htmls...
<div id='client_info'>
<p><b>[client_name]</b><br/><b>[client_company]</b></p>
<p>[client_address]<br/>[client_CP]<br/>[client_city]</p>
</div>
...more htmls...
'''
soup = BeautifulSoup(html)
div = soup.find("div", {"id":"client_info"})
p = div.findAll("p")
for tag in p:
print re.findall('\[([^\]]*)\]', tag.renderContents())
我有一个html文件,其中包含[]
之间的变量,我想获取其中的每个单词
<div id='client_info'>
<p><b>[client_name]</b><br/><b>[client_company]</b></p>
<p>[client_address]<br/>[client_CP]<br/>[client_city]</p>
</div>
但它输出('client\u name]
[client\u company',)
我试过玩^
和$
,但没有成功
感谢您的帮助。使用非贪婪量词,如下所示:
re.search('\[(.*?)\]', html_template)
re.search('\[([^\]]*)\]', html_template)
或者一个字符类,如下所示:
re.search('\[(.*?)\]', html_template)
re.search('\[([^\]]*)\]', html_template)
和用于获取所有匹配的子字符串。Python有一个非常强大的库,名为。我建议您使用此库解析html。因此,我建议您首先使用此库解析
div
。然后执行正则表达式
html = '''
...some htmls...
<div id='client_info'>
<p><b>[client_name]</b><br/><b>[client_company]</b></p>
<p>[client_address]<br/>[client_CP]<br/>[client_city]</p>
</div>
...more htmls...
'''
soup = BeautifulSoup(html)
div = soup.find("div", {"id":"client_info"})
p = div.findAll("p")
for tag in p:
print re.findall('\[([^\]]*)\]', tag.renderContents())
html=''
…一些HTML。。。
[客户名称]
[客户公司]
[客户地址]
[客户地址]
[客户城市]
…更多htmls。。。
'''
soup=BeautifulSoup(html)
div=soup.find(“div”,“id”:“client_info”})
p=div.findAll(“p”)
对于p中的标记:
打印re.findall('\[([^\]]*)\]',tag.renderContents())
可能有一种方法可以使用解析
,但我不知道。看看这个,你会明白:这只给了我一个结果:('client_name',)
@Loïc使用re.findall()
,方式类似