Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.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/17.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-re-正则表达式需要帮助吗_Python_Regex - Fatal编程技术网

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()
,方式类似