Search code examples
pythonexchangelib

Apply Regex in filter() in exchangelib


I am working on a script that needs to filter subject using regex. Does exchangelib support that? If so, can I get some examples?


Solution

  • Regular expressions are not supported in EWS, so you can't do the filtering server-side. You'll have to pull all items and do the filtering client-side:

    for item in account.inbox.all():
        if re.match(r'some_regexp', item.subject):
            # Do something
    

    If you expect to match only very few items, you could optimize by first fetching only the subject field, and then full items:

    matches = []
    for item in account.inbox.all().only('subject'):
        if re.match(r'some_regexp', item.subject):
            matches.append(item)
    full_items = account.fetch(matches)