|
Regular Expressions in Delphi
Copyright © 2000 Ernesto
De Spirito
What are regular expressions?
Regular Expressions are a way to search and replace patters of text. In a way,
these patterns are like using the wildcards '?' and '*' when
searching for files, but regular expressions are immensely more powerful
than that. In the pattern you can specify whether the string being
sought should occur at the beginning or end of the line, which
characters are allowed, how many times they can be repeated and many
more things.
What are they used for?
Basically, they are used for three purposes:
Powerful text search (and replace)
You can use regular expressions for example to search for a specified
record in a database in a much more powerful way than using the SQL
LIKE operator.
Data validation
With Regular Expressions you can validate user input to check whether
it follows a given format that due to its complexity and variability
cannot be checked with masks. For example, you can check whether a
string starts optionally with a plus or minus sign, then a sequence
of no more than seven digits and optionally a point followed by up to
two digits.
Data extraction
Not only you can check whether a string follows a certain pattern,
but you can extract arbitrary parts of the text. For example, for a
phone number like '+1 (123) 555-9999' you can extract the country
code ('1'), area code ('123') and the local number ('555-9999').
TRegExpr freeware library
Want to implement regular expressions in your Delphi applications? A
good way to do it is using the TRegExpr library, which is freeware and
comes with full source code and is documented in many languages. This
library was developed by Andrey V. Sorokin porting the C code from the
well-known Henry Spencer's V8-routines (a subset of Perl Regular
Expressions) to Object Pascal. The last version of the library is 0.942
and you can get it from "AnSo @ Web" (Sorokin's web site) at:
Examples
The syntax of V8 regular expressions is well documented in the help
file and you can also find many sources of documentation in the
Internet, so we are not going to enter into that, but just show a
couple of examples of the usage of the library:
uses RegExpr;
procedure TForm1.Button1Click(Sender: TObject);
// Validates the email address in Edit1
begin
// Warning: this code should not be used to perform actual
// email validation. You should check the RFC specification.
// This is just a simplification to show the use of ExecRegExpr.
if not ExecRegExpr('[\w\d\-\.]+@[\w\d\-]+(\.[\w\d\-]+)+',
Edit1.Text) then begin
ShowMessage('The email address is not valid');
Edit1.SetFocus;
end else
ShowMessage('The email address is valid');
end;
procedure TForm1.Button2Click(Sender: TObject);
// Extracts email addresses contained in Memo1
var
RegExpr: TRegExpr;
begin
// Warning: this code will not extract all valid email addresses.
// This is just a simplification to show the use of Exec and Match.
ListBox1.Clear;
RegExpr := nil;
try
RegExpr := TRegExpr.Create;
if RegExpr <> nil then begin
RegExpr.Expression := '[^\w\d\-\.]([\w\d\-\.]+@[\w\d\-]+'
+ '(\.[\w\d\-]+)+)[^\w\d\-\.]';
if RegExpr.Exec(Memo1.Text) then
repeat
ListBox1.Items.Add(RegExpr.Match[1]);
until not RegExpr.ExecNext;
end;
except
end;
RegExpr.Free;
end;
You can find the full source code of this article in the archive that accompanies the Pascal Newsletter #21.
|