Jump to content
Ian Branch

Regex help please..

Recommended Posts

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

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;

 

  • Like 1

Share this post


Link to post

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
  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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×