regex - 正则表达式 ”?!” 有变量
问题描述
我正在测试用于互联网爬行的代码。
def getExternalLinks(bs, excludeUrl):
externalLinks = []
#Finds all links that start with "http" that do
#not contain the current URL
for link in bs.find_all('a',
href=re.compile('^(http|www)((?!'+excludeUrl+').)*$')):
if link.attrs['href'] is not None:
if link.attrs['href'] not in externalLinks:
externalLinks.append(link.attrs['href'])
return externalLinks
我无法分析re.compile('^(http|www)((?!'+excludeUrl+').)*$')) 中的正则表达式((?!'+excludeUrl+'). )
解决方案
检查文档:
(?!...)
如果 ... 下一个不匹配,则匹配。这是一个否定的前瞻断言。例如,Isaac (?!Asimov) 将匹配 'Isaac ' 仅当它后面没有 'Asimov' 时。
推荐阅读
- javascript - 将日期“2020 年 8 月 18 日星期二 12:30:44 GMT+0530”转换为“2020 年 8 月 18 日星期二 12:30:44 GMT+05:30”
- javascript - 我们可以在自适应卡片中添加多少个提及实体?
- vba - 我们可以通过名称访问单词表而不是使用 vba 索引吗?
- python - 将特定日期作为默认日期时间添加到 Django 中的 DateTimeField
- python - 运行 CTC 损失函数
- angular - Angular 和 Stripe 集成
- python - 如何在字符串中仅保留小写文本和下划线并用空格替换其他所有内容?
- java - 如何正确读取 servlet 中的表单数据?
- ruby-on-rails - 在 ubuntu 18.04 上将数据库 postgresql 连接到 ruby
- r - 处理从 R 到 Tableau 的字符串返回并将其拆分