Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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 将html标记与条件正则表达式匹配_Python_Regex - Fatal编程技术网

Python 将html标记与条件正则表达式匹配

Python 将html标记与条件正则表达式匹配,python,regex,Python,Regex,我试图使用条件正则表达式来匹配或 我的需求表达式如下: <label(\s?)(?(1)\w+)> 因此,我能够将标签与属性相匹配,但如何也匹配普通标签呢?非常感谢 您希望[^>]*匹配所有内容,直到您点击 re.search('<label([^>]*)>','<label title=>') # matches re.search('<label([^>]*)>','<label>') # matches re.se

我试图使用条件正则表达式来匹配

我的需求表达式如下:

<label(\s?)(?(1)\w+)>

因此,我能够将标签与属性相匹配,但如何也匹配普通标签呢?非常感谢

您希望
[^>]*
匹配所有内容,直到您点击

re.search('<label([^>]*)>','<label title=>') # matches
re.search('<label([^>]*)>','<label>') # matches
re.search(']*)>,'')#匹配
重新搜索(']*)>,'')#匹配项

您希望
[^>]*
匹配所有内容,直到您点击

re.search('<label([^>]*)>','<label title=>') # matches
re.search('<label([^>]*)>','<label>') # matches
re.search(']*)>,'')#匹配
重新搜索(']*)>,'')#匹配项

您只需使用OR运算符(
|
):

re.search('|','')#匹配
重新搜索(“|”和“)#匹配项

您只需使用OR运算符(
|
):

re.search('|','')#匹配
重新搜索(“|”和“)#匹配项

有效,但我希望有一个更优雅的解决方案,尽管当时我想不出这个答案:)有效,但我希望有一个更优雅的解决方案,尽管当时我想不出这个答案:)
re.search('<label(\s?)(?(1)\w+)>|<label>','<label>') # matches
re.search('<label(\s?)(?(1)\w+)>|<label>','<label title=>') # matches