Ian Branch 127 Posted September 19, 2021 Hi Team, Regex is one of those mysteries I have never fathomed. :-( I have the following code.. function ValidEmail(const EmailAddress: string): Boolean; const EMAIL_REGEX = '^((?>[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])' + '[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)' + '(?>\.?[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])' + '[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]' + '{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d))' + '{4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\' + '[\x01-\x7f])+)\])(?(angle)>)$'; begin Result := IsMatch(EmailAddress, EMAIL_REGEX); end; I don't recall exactly where I found it, apologies to the author, and it has served me well. It works well for single email address entries. How can it be modified so that it will parse multiple email addresses separated by a ';'? e.g. myemail@here.com;youremail@there.com.au With possible expansion to three or more email addresses in the future. I will be most grateful for any solution. Regards & TIA, Ian Share this post Link to post
corneliusdavid 214 Posted September 19, 2021 I think I would split the string on semicolons and run the regex on each individual address. function ValidEmail(const EmailAddress: string): Boolean; const EMAIL_REGEX = '^((?>[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])' + '[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)' + '(?>\.?[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])' + '[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]' + '{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d))' + '{4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\' + '[\x01-\x7f])+)\])(?(angle)>)$'; var Emails: TStringList; begin Result := True; Emails := TStringList.Create; try Emails.Delimiter := ';'; Emails.StrictDelimiter := True; Emails.DelimitedText := EmailAddress; for var i := 0 to Emails.Count - 1 do if not IsMatch(Emails[i], EMAIL_REGEX) then begin Result := False; Break; end; finally Emails.Free; end; end; 2 Share this post Link to post
Ian Branch 127 Posted September 19, 2021 Hi David, Doh!! This is where I face palm myself and berate myself for not thinking of that. 😞 Thank you for pointing out my inadequacy. 🙂 Regards & Tks again, Ian Share this post Link to post
corneliusdavid 214 Posted September 19, 2021 LOL! We all get stuck in a train of thought once in a while! I've definitely been there. 😎 Share this post Link to post
Darian Miller 361 Posted September 19, 2021 Why don't you extract the email addresses and process them individually? Also look at: https://github.com/Xor-el/EmailValidationPascal Share this post Link to post
Attila Kovacs 629 Posted September 19, 2021 EMAIL_REGEX = '^((((?>[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])' + // '[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)' + // '(?>\.?[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])' + // '[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]' + // '{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d))' + // '{4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\' + // '[\x01-\x7f])+)\])(?(angle)>))(;\S)*)+$'; eventually Share this post Link to post
Ian Branch 127 Posted September 20, 2021 Hi Attila, Thank you for your contribution. Given that I have no idea about regex I can only wonder at what you have done. Regards, Ian Share this post Link to post
Fr0sT.Brutal 900 Posted September 20, 2021 ...and then International Domain Names come into chat 1 Share this post Link to post
Timothy.Prinsloo 0 Posted December 13, 2022 @Fr0sT.Brutal speaking of International Domain Names any ideas I still have to validate the Latin alphabet with diacritics Pelé@example.com Greek alphabet δοκιμή@παράδειγμα.δοκιμή Traditional Chinese characters 我買@屋企.香港 Japanese characters 二ノ宮@黒川.日本 Cyrillic characters медведь@с-балалайкой.рф Devanagari characters संपर्क@डाटामेल.भारत using the same RegEx as OP Share this post Link to post
Lars Fosdal 1792 Posted December 13, 2022 On 9/19/2021 at 11:53 PM, Attila Kovacs said: EMAIL_REGEX = '^((((?>[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])' + // '[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)' + // '(?>\.?[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])' + // '[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]' + // '{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d))' + // '{4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\' + // '[\x01-\x7f])+)\])(?(angle)>))(;\S)*)+$'; eventually You have a problem. You apply RegEx to solve the problem. You now have two problems. 1 4 Share this post Link to post
Attila Kovacs 629 Posted December 13, 2022 (edited) 17 minutes ago, Lars Fosdal said: You now have two problems. \p{L}+([-+.']\p{L}+)*@\p{L}+([-.]\p{L}+)*\.\p{L}+([-.]\p{L}+)*$ the devanagari fails Edited December 13, 2022 by Attila Kovacs 1 Share this post Link to post
Timothy.Prinsloo 0 Posted December 13, 2022 @Attila Kovacs just out of interest does work in Delphi... I know it works for C# but isn't the regex syntax different between the two languages \p{L}+([-+.']\p{L}+)*@\p{L}+([-.]\p{L}+)*\.\p{L}+([-.]\p{L}+)*$ Share this post Link to post
Attila Kovacs 629 Posted December 13, 2022 44 minutes ago, Timothy.Prinsloo said: out of interest there is a PCRE regex in newer Delphi releases, yes, at some degree it will work 1 Share this post Link to post
Sherlock 663 Posted December 14, 2022 Given the complexity and the tendency of developers to always suggest some totally different approach , here is my completely different suggestion. You seem to have the need to validate an e-mail address before storing it. How about sending a test mail before doing that. That way you get to birds with one stone: Verify the address, and get the permission to use it... a must have in Europe. 1 Share this post Link to post
Lajos Juhász 293 Posted December 14, 2022 10 minutes ago, Sherlock said: Given the complexity and the tendency of developers to always suggest some totally different approach , here is my completely different suggestion. You seem to have the need to validate an e-mail address before storing it. How about sending a test mail before doing that. That way you get to birds with one stone: Verify the address, and get the permission to use it... a must have in Europe. This is an excellent suggestion. I doubt it is a must in Europe, I still receive sensitive information for other people in my e-mail account. Share this post Link to post
Attila Kovacs 629 Posted December 14, 2022 20 minutes ago, Sherlock said: How about sending a test mail before doing that It's a very-very bad suggestion. You should read an article how not to get into a spam db. First, you have to avoid sending emails to malformed email addresses. 20 minutes ago, Sherlock said: a must have in Europe Nope. Also, there is a difference between private/non ~ emails, first / second+ contact, etc.... Share this post Link to post
Fr0sT.Brutal 900 Posted December 14, 2022 I'd rather ask myself WHY I need such validation. Email "rubba@hubba.bubba" will pass any checks but won't gain any sense. Domain check? They're multiplying like rabbits so you'll have to keep regex up to date + it won't save from "rubba@hubba.com" emails. 1 Share this post Link to post
Attila Kovacs 629 Posted December 14, 2022 3 minutes ago, Fr0sT.Brutal said: I'd rather ask myself WHY I need such validation I have it for two reasons, first, ppls. manual input is messy, second, if they copy-paste from websites it can be a strange thing in the edit field and they don't look. Share this post Link to post
Fr0sT.Brutal 900 Posted December 14, 2022 4 minutes ago, Attila Kovacs said: I have it for two reasons, first, ppls. manual input is messy, second, if they copy-paste from websites it can be a strange thing in the edit field and they don't look. For these purposes simple NonSpace{1,}@NonSpace{1,}.NonSpace{2,} will be enough IMHO Share this post Link to post
Attila Kovacs 629 Posted December 14, 2022 23 minutes ago, Fr0sT.Brutal said: For these purposes simple NonSpace{1,}@NonSpace{1,}.NonSpace{2,} will be enough IMHO Yup. Share this post Link to post
Timothy.Prinsloo 0 Posted December 14, 2022 @Attila Kovacs & @Lars Fosdal Thanks for all the help. I went with this as my solution EMAIL_REGEX = '^((((?>[a-zA-Z\d!#$%&’“”''*+\-\/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])' +'[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)' +'(?>\.?[a-zA-Z\d!#$%&’“”''*+\-\/=?^_`{|}~]+)+|"((?=[\x01-\x7f])' +'[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|(((?(?<!)\.)' +'(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])' +'[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>))(;\S)*)+$|' +'(\p{L}+([-+.]\p{L}+)*@\p{L}+([-.]\p{L}+)*\.\p{L}+([-.]\p{L}+)*$)'; Share this post Link to post
dummzeuch 1505 Posted December 14, 2022 Does that validate Thomas.Müller-Lüdenscheid+privat@abslutseriös.de ? And also the Punycode version of it? Share this post Link to post
Sherlock 663 Posted December 15, 2022 On 12/14/2022 at 11:44 AM, Attila Kovacs said: First, you have to avoid sending emails to malformed email addresses. I would have put that check into an SMTP server. And...surprisingly the good ones do that. Share this post Link to post