Fix the email check for untrusted comments
Your task
Your service pulls email addresses out of free-text comments that anyone can post. An assistant's first regex matched every example the team tried, but nobody could say what it accepted. The team asked for a check whose rules can be read and tested.
An assistant wrote is_plausible_email(candidate). It is in the editor. It has a bug. Find it and fix it.
Input and output:
candidate: the text to check.- Return true when it is a plausible email address, false otherwise.
Rules the tests check:
- There is exactly one
@. It splits the text into the local part, before it, and the domain, after it. - The local part is 1 to 64 characters. Each is an ASCII letter, a digit from 0 to 9,
.,+,_or-. It does not start or end with., and never has two.in a row. - The domain is 1 to 255 characters. Split on
., it has at least 2 parts, called labels. - Each label is not empty, uses only ASCII letters, digits and
-, and does not start or end with-. - The last label is at least 2 characters long and is all letters.
- Any other character, a space included, makes the text not plausible.
Examples
is_plausible_email(candidate: str) -> boolExample 1
Input
is_plausible_email("user+tag@example.co.uk")Output
TrueExample 2
Input
is_plausible_email("user@@example.com")Output
FalseExample 3
Input
is_plausible_email("user@example")Output
False