regex - 如何在python3中组合两个re.compile正则表达式?

标签 regex python-3.x beautifulsoup pydev

我想将两个正则表达式组合成一行。

 soup1=link.findAll('a', attrs={'href': re.compile('^http://')})
 soup2=link.findAll('a', attrs={'href': re.compile("/news/")})

我尝试了 (|) 登录 re.compile('^http://' | '/news/') 但一切都是徒劳。我需要这两个功能(包含 'http' 以及 /news/ 的链接)

最佳答案

您不需要正则表达式,您可以使用css选择器:

 soup.select('a[href^=http://],a[href*=/news/]')

^= 查找以子字符串开头的 href,*= 查找在任意位置包含子字符串的 href。

关于regex - 如何在python3中组合两个re.compile正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38322393/

相关文章:

sql - 选择 Where Like 正则表达式

javascript - 将 _wrapper 替换为 html <i> 标签

java - String 中的 4 个子字符串

python - 属性错误: 'NoneType' object has no attribute 'dtype'

python - 尝试使用 BeautifulSoup 打印表格的单行,但行位置不断变化

python - ValueError:以 10 为底的 int() 无效文字: '196.41'

python - 属性错误: 'list' object has no attribute 'h3' ( Beautifulsoup )

javascript - 让正则表达式找到 bbcode 中的最小组

python - 如果字段名称与 python 中的字典值匹配,则求和

python-3.x - Flask-Admin 在数据库中存储 NULL 而不是空字符串 ('' )