Using the TRegExpr Class

We used the Free Pascal Wiki to get started with regular expressions in Lazarus and the examples page of the useful website by Jan Goyvaerts for the regular expression. Jan neatly describes regular expressions as "wildcards on steroids". On another website you can have regular expressions tested and explained and then make changes accordingly. Regular expressions are most helpful for validation, and our little console example validates email addresses.

program RegexDemo;

uses
  regexpr;
var
  RegexObj: TRegExpr;
  email: string;
begin
  RegexObj := TRegExpr.Create;
  RegexObj.ModifierI := true; //case insensitive
  RegexObj.Expression := '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$';
  repeat
    write('Please enter an email address (q to quit): ');
    readln(email);
    if (email = 'q') or (email = 'Q') then
      begin
        RegexObj.Free;
        halt;
      end;
    if RegexObj.Exec(email) then
      writeln('Valid email address')
    else
      writeln('Invalid email address');
  until false;
end.
Programming - a skill for life!

Demonstrations of TStringList, TFileString, TQueue and TRegExp and drawing on a canvas