Search Results for

    Show / Hide Table of Contents

    Regular expressions (Delphi)

    Note

    This demo is available in your FlexCel installation at <FlexCel Install Folder>\Demo\Delphi\Modules\20.Reports\47.Regular Expressions In Reports and also at https:​//​github.​com/​tmssoftware/​TMS-​FlexCel.​VCL-​demos/​tree/​master/​Delphi/​Modules/​20.​Reports/​47.​Regular Expressions In Reports

    Overview

    Regular expressions give you both the power to transform the text in your reports in any way you like, and also to obfuscate and make unmaintainable your templates if you abuse them. The <#regex()> tag lets you write your own Regular Expressions in a FlexCel report.

    Concepts

    • There are 2 versions of the <#regex> tag:

    • RegEx(IgnoreCase, Expression, Search): This will search for the expression inside "Search" string, and return the part of "Search" that matches the expression.

    • RegEx(IgnoreCase, Expression, Search, Replace): This will Replace the matching string inside "Search" by the "Replace" string.

    • Both "Search" and "Replace" parameters are evaluated at runtime, so they will evaluate to different strings as the row changes. The "Expression" parameter is evaluated only once before starting the report, so it will not change.

    • As formerly said, use regular expressions with care. Sometimes they are just invaluable to get your desired output, but on other times they can make maintaining the templates a lot of work. Depending on the case, you might find better to define your own user function to do a complex transformation. (See the User Defined Functions example)

    • Regular Expressions are a complex subject, and we will not explain how to use them here. There is a lot of information out there available on them.

    Files

    UMainForm.pas

    unit UMainForm;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics,
      FlexCel.VCLSupport, FlexCel.Core, FlexCel.XlsAdapter, FlexCel.Report, FlexCel.Render,
      {$if CompilerVersion >= 23.0} System.UITypes, {$IFEND}
      ShellApi,
      Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
    
    type
      TMainForm = class(TForm)
        btnCancel: TButton;
        btnGo: TButton;
        SaveDialog: TSaveDialog;
        Label1: TLabel;
        procedure btnCancelClick(Sender: TObject);
        procedure btnGoClick(Sender: TObject);
      private
        procedure RunReport;
        function GetDataPath: string;
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      MainForm: TMainForm;
    
    implementation
    uses IOUtils, DemoData;
    
    {$R *.dfm}
    
    procedure TMainForm.btnCancelClick(Sender: TObject);
    begin
      Close;
    end;
    
    procedure TMainForm.btnGoClick(Sender: TObject);
    begin
      RunReport;
    
    end;
    
    function TMainForm.GetDataPath: string;
    begin
      Result := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..');
    end;
    
    
    procedure TMainForm.RunReport;
    var
      Report: TFlexCelReport;
    begin
      if not SaveDialog.Execute then exit;
    
      Report := TFlexCelReport.Create(true);
      try
        Report.AddTable(DemoTables);
        Report.SetValue('Date', Now);
        Report.SetValue('ReportCaption', 'Suppliers');
        Report.Run(
          TPath.Combine(GetDataPath, 'Regular Expressions In Reports.template.xls'),
          SaveDialog.FileName);
      finally
        Report.Free;
      end;
    
      if MessageDlg('Do you want to open the generated file?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
      begin
        ShellExecute(0, 'open', PCHAR(SaveDialog.FileName), nil, nil, SW_SHOWNORMAL);
      end;
    
    
    end;
    
    end.
    
    In This Article
    Back to top FlexCel Studio for VCL and FireMonkey v7.24
    © 2002 - 2025 tmssoftware.com