I wanted to make a simple email address validator using StringMatchQ. This was my first attempt:
simplifiedEmailPattern = StringExpression[
Repeated[
WordCharacter | "-" | "_" | "." | "+",
{1, 255}
],
Verbatim["@"],
StringExpression[
LetterCharacter,
Repeated[WordCharacter],
Repeated[
StringExpression[
"." | "-",
LetterCharacter,
Repeated[WordCharacter]
]
]
]
]
This works as expected for a few simple test cases:
StringMatchQ["[email protected]", simplifiedEmailPattern] (* True *)
StringMatchQ["user@baddomain", simplifiedEmailPattern] (* False*)
StringMatchQ["no_at_symbol.com", simplifiedEmailPattern] (* False *)
Then I discovered that Interpreter has a built-in validator which is probably more robust than anything I can make by hand, so I tried doing this:
interpreterEmailPattern = (string___ /; !FailureQ[Interpreter["EmailAddress"][string]])
which gives the same results for the above tests:
StringMatchQ["[email protected]", interpreterEmailPattern] (* True *)
StringMatchQ["user@baddomain", interpreterEmailPattern] (* False *)
StringMatchQ["no_at_symbol.com", interpreterEmailPattern] (* False *)
The problem is when I run profiling, the Interpreter method is MUCH slower (more than 200 times slower in fact):
In[91]:= First@
Timing[Table[StringMatchQ["[email protected]", simplifiedEmailPattern],
10000]]
Out[91]= 0.03125
In[92]:= First@
Timing[Table[StringMatchQ["[email protected]", interpreterEmailPattern],
10000]]
Out[92]= 7.3125
I suspect the time difference is because Interpreter is doing more than just a validation, it is also doing some kind of interpretation. But for my email address validator, I don't care about the interpretation.
My question is: Is there a way to user whatever pattern Interpreter is using to validate email addresses directly?
--
NOTE: The doc for "EmailAddress" Interpreter type states: "Use of 'EmailAddress' does not require connectivity to the Wolfram Cloud." So that is not the cause of the delay.