Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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/19.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_Beautifulsoup - Fatal编程技术网

Python 正则表达式/

Python 正则表达式/,python,regex,beautifulsoup,Python,Regex,Beautifulsoup,我正在尝试将正则表达式合并到我的bs4项目中 <a href="/event/football">football</a> <a href="/event/rugby-union">Rugby Union</a> <a href="/event/ladies/football">Ladies Soccer</a> <a href="/event/womens/rugby-union">Womens Rugby&l

我正在尝试将正则表达式合并到我的bs4项目中

<a href="/event/football">football</a>
<a href="/event/rugby-union">Rugby Union</a>
<a href="/event/ladies/football">Ladies Soccer</a>
<a href="/event/womens/rugby-union">Womens Rugby</a>

您可能需要在字符集之后添加一个
+
,以便它匹配多个字符:

for event in events.find_all('a', href=re.compile('^/event/[^/]+'))
#                                                              ^

您可能需要在字符集之后添加一个
+
,以便它匹配多个字符:

for event in events.find_all('a', href=re.compile('^/event/[^/]+'))
#                                                              ^

要匹配任何只包含2个部分的href,您需要

r'^/event/[^/]+$'
              ^^

+
量词匹配一个或多个量化子模式,而
$
锚点匹配字符串的结尾,因此不允许有更多的部分

若要禁止所有以
-cancelled
结尾的href值,请添加一个负的前瞻(
(?!.-cancelled$)
,如果
/event/
后跟任何0+字符,并在字符串(
$
)结尾处后跟
-cancelled
),则匹配将失败:

()或使用lookbehind(到达字符串末尾后将执行一次
(?),如果字符串末尾有
-取消的
,则匹配将失败):


r'^/event/[^/]+$(?要匹配任何只包含2个部分的href,您需要

r'^/event/[^/]+$'
              ^^

+
量词匹配一个或多个量化子模式,而
$
锚点匹配字符串的结尾,因此不允许有更多的部分

若要禁止所有以
-cancelled
结尾的href值,请添加一个负的前瞻(
(?!.-cancelled$)
,如果
/event/
后跟任何0+字符,并在字符串(
$
)结尾处后跟
-cancelled
),则匹配将失败:

()或使用lookbehind(到达字符串末尾后将执行一次
(?),如果字符串末尾有
-取消的
,则匹配将失败):


r'^/event/[^/]+$(?尝试
r'^/event/[^/]+$”
,效果非常好,谢谢。还有一件事,我还想排除和href,并在其中添加取消的单词。我尝试添加[^/cancelled]但是排除了任何包含c或a等的内容。我如何将取消添加为一个词?你的意思是你想匹配任何
/event/…
/event/cancelled
?任何事件除非以-cancelled结尾,否则事件/橄榄球联盟将匹配,但事件/橄榄球联盟取消将看不到我的详细答案。请尝试
r'^/event/[^/]+$'
非常好,谢谢。还有一件事,我还想排除和href,并在其中添加单词cancelled。我尝试添加[^/cancelled]但是排除了任何包含c或a等的内容。我如何将取消添加为一个词?你的意思是你想匹配任何
/event/…
/event/cancelled
?任何事件除非以-cancelled结尾,否则事件/橄榄球联盟将匹配,但事件/橄榄球联盟取消将看不到我的详细答案。
r'^/event/[^/]+$(?<!-cancelled)'