imaplib找到完整的主题字符串但不是部分的 – 如何修复?

huangapple go评论67阅读模式
英文:

imaplib finds the full subject string but not partial - how to fix?

问题

I'm using impalib to connect to my gmail:

我正在使用impalib连接到我的Gmail:

import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('bi@great_company', 'app_password')
mail.select('"[Gmail]/All Mail"', readonly=True)

This finds an email:

这会找到一封邮件:

mail.search(None, 'SUBJECT "Green Law_Settlement_12242022"')

The email subject is: "Green Law_Settlement_12242022.xlsx"

邮件主题是:“Green Law_Settlement_12242022.xlsx”

But this does not return any match:

但是这不会返回任何匹配项:

mail.search(None, 'SUBJECT "Green Law_Settlement_122"')

I need all the mails where the subject starts with "Green Law_Settlement_". Any way of doing that?

我需要所有主题以“Green Law_Settlement_”开头的邮件。有没有办法做到这一点?

英文:

I'm using impalib to connect to my gmail:

import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('bi@great_company', 'app_password')
mail.select('"[Gmail]/All Mail"', readonly=True)

this finds an email:

mail.search(None, 'SUBJECT "Green Law_Settlement_12242022"')

the email subject is: "Green Law_Settlement_12242022.xlsx"

but this does not return any match:

mail.search(None, 'SUBJECT "Green Law_Settlement_122"')

I need all the mails where the subject starts with "Green Law_Settlement_" any way of doing that?

答案1

得分: 1

IMAP说搜索匹配子字符串,并且不区分大小写,但它并没有定义"substring"。我认为RFC的作者假设字符串通常是字符的序列。然而,Gmail说字符串是单词的序列,单词的确切定义并不重要。"Color"和"colour"可以被视为相同的单词,"colours"或"colouring"也可以。

Gmail的方式并不独特。使用SOLR或Lucene内部的IMAP服务器会与Gmail类似,例如。这对于在服务器内部实现搜索的代码非常有意义。

结论是尽量使用由整个单词组成的子字符串。使用"green law"进行搜索应该在所有服务器上都可以,但使用"en law_set"则有风险。

英文:

IMAP says searches match substrings, and case-insensitively, but it doesn't define "substring". I think the author of the RFC assumed that strings are a sequence of characters, as they commonly are. However, gmail says that strings are a sequence of words, and a word is… the precise definition doesn't matter. Color and colour may be regarded as the same word, as may colours or colouring.

Gmail's way isn't unique. The IMAP servers that use SOLR or Lucene internally will work somewhat like gmail, for example. It makes a lot of sense for the code that implements searching inside the server.

The conclusion is to try to use substrings that consist of entire words. Searching for "green law" should be fine with all servers, for "en law_set" is risky.

huangapple
  • 本文由 发表于 2023年4月17日 16:57:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定