Regex to validate SMTP Responses?
- by Alix Axel
I'm writing a regular expression that can interactively validate SMTP responses codes, once the SMTP dialog is completed it should pass the following regex (some parentheses added for better readability):
^(220)(250){3,}(354)(250)(221)$
Or with(out) authentication:
^(220)(250)((334){2}(235))?(250){2,}(354)(250)(221)$
I'm trying to do rewrite the above regexes so that I can interactively check if the dialog is going as expected, otherwise politely send a QUIT command and close the connection saving bandwidth and time, but I'm having a hard time writing an optimal regex. So far I've managed to come up with:
^(220(250(334(235(250(354(250(221)?)?)?){0,})?){0,2})?)?$
Which, besides only matching authenticated connections, has some bugs... For instance, it matches:
220250334235250354250221
220250334334235250354250221
I've also tried the following modification:
^(220(250)?)?((334(235)?){2})?(250(354(250(221)?)?)?){0,}$
This one accepts non-authenticated responses but it fails to match 220250334 and wrongly matches 220250334334235250354250221 (at least 2 250 are needed before the 354 response code).
Can someone help me out with this? Thanks in advance.