You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在exchangelib的filter()中使用正则过滤邮件主题的技术咨询

Does exchangelib support regex filtering for email subjects?

Great question! exchangelib doesn’t natively support regex-based filtering directly through EWS (Exchange Web Services) queries, but you can easily work around this by pairing exchangelib’s search tools with Python’s built-in re module. This gives you the flexibility to handle even complex pattern matching for email subjects.

Approach 1: Fetch emails first, then regex filter

This is the most straightforward method—pull emails from your mailbox (with optional basic filtering to reduce volume) and then apply your regex pattern to the subject line.

import re
from exchangelib import Credentials, Account

# Set up your Exchange account connection
credentials = Credentials('your_email@domain.com', 'your_password')
account = Account('your_email@domain.com', credentials=credentials, autodiscover=True)

# Define your regex pattern (example: match subjects containing "Invoice" followed by numbers)
subject_pattern = re.compile(r'.*Invoice\s+\d+.*', re.IGNORECASE)

# Fetch recent emails (adjust the slice to limit results, e.g., [:200] for last 200)
matched_emails = []
for email in account.inbox.all().order_by('-datetime_received')[:100]:
    if subject_pattern.search(email.subject):
        matched_emails.append(email)
        print(f"Matched email: {email.subject}")

Approach 2: Narrow results with EWS filters first, then regex

To reduce network traffic and speed up processing, use exchangelib's Q filters to first narrow down emails to a smaller set (e.g., subjects containing a keyword), then apply regex for finer-grained matching.

import re
from exchangelib import Credentials, Account, Q

# Set up account connection (same as above)
credentials = Credentials('your_email@domain.com', 'your_password')
account = Account('your_email@domain.com', credentials=credentials, autodiscover=True)

# First, filter emails with EWS to get subjects containing "Invoice"
pre_filtered_emails = account.inbox.filter(
    Q(subject__contains='Invoice')
).order_by('-datetime_received')

# Apply regex to find subjects with numbers after "Invoice"
number_pattern = re.compile(r'Invoice\s+(\d+)', re.IGNORECASE)
for email in pre_filtered_emails:
    match = number_pattern.search(email.subject)
    if match:
        print(f"Matched invoice email with number: {match.group(1)} | Subject: {email.subject}")

A quick note on EWS limitations

EWS itself doesn’t support full regex in search queries—it only allows simple wildcard matching (using * for multiple characters, ? for single characters). For example, you could do Q(subject__like='*Invoice*') but this can’t handle complex patterns like number sequences or case-insensitive partial matches as reliably as Python’s re module.

For most use cases, combining EWS basic filters with Python regex is the best balance of performance and flexibility.

内容的提问来源于stack exchange,提问作者Ladu anand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:55:10