Johan Åhlgren

Using regular expressions in Delphi

For a while now, we have had support for regular expressions in Delphi. This came in handy when I needed a way to extract all links from a HTML page. Turns out it was not too difficult, and the following code snippet takes care of it:

procedure ProcessLinks(const Html: String);
var
  RegEx: TRegEx;
  Match: TMatch;
begin
  RegEx := TRegEx.Create(
             '.*?', 
             [roIgnoreCase, roMultiline]
           );
  for Match in RegEx.Matches(Html) do
    if Match.Success and (Match.Groups.Count > 1) then
      WriteLn('LINK:' + Match.Groups[1].Value);
  WriteLn('DONE');
end;

Leave a Reply

Your email address will not be published.