Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.1.0+

Regular Expression with Multiple Matches and Named Capture Groups

See more Regular Expressions Examples

Demonstrates regular expressions with named capture groups and multiple matches.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.JsonObject,
  Chilkat.StringBuilder;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sb: TStringBuilder;
  crlf: Boolean;
  pattern: string;
  json: TJsonObject;
  timeoutMs: Integer;
  numMatches: Integer;
  idx_first: Integer;
  idx_last: Integer;
  i: Integer;
  matchCount: Integer;

begin
  success := False;

  sb := TStringBuilder.Create;
  crlf := True;
  sb.AppendLine('Name: John Smith',crlf);
  sb.AppendLine('Name: Jack Johnson',crlf);
  sb.AppendLine('Name: Mary Adams',crlf);

  WriteLn(sb.GetAsString());

  //  We have the following string:
  //  Name: John Smith
  //  Name: Jack Johnson
  //  Name: Mary Adams

  pattern := 'Name:\\s+(?<first>\\w+)\\s+(?<last>\\w+)';
  json := TJsonObject.Create;
  json.EmitCompact := False;

  timeoutMs := 2000;
  numMatches := sb.RegexMatch(pattern,json,timeoutMs);
  if (numMatches < 0) then
    begin
      //  Probably an error in the regular expression.
      //  Suggestion: Use AI to help create and/or diagnose regular expressions.
      WriteLn(sb.LastErrorText);
      Exit;
    end;

  //  Examine the matches:
  WriteLn(json.Emit());

  //  Here is the JSON showing the matches.
  //  Important:  Capture group 0 always contains the entire match — that is, the portion of the input string that matches the full regular expression.

  //  {
  //    "named": {
  //      "first": 1,
  //      "last": 2
  //    },
  //    "match": [
  //      {
  //        "group": [
  //          {
  //            "cap": "Name: John Smith",
  //            "idx": 0,
  //            "len": 16
  //          },
  //          {
  //            "cap": "John",
  //            "idx": 6,
  //            "len": 4
  //          },
  //          {
  //            "cap": "Smith",
  //            "idx": 11,
  //            "len": 5
  //          }
  //        ]
  //      },
  //      {
  //        "group": [
  //          {
  //            "cap": "Name: Jack Johnson",
  //            "idx": 18,
  //            "len": 18
  //          },
  //          {
  //            "cap": "Jack",
  //            "idx": 24,
  //            "len": 4
  //          },
  //          {
  //            "cap": "Johnson",
  //            "idx": 29,
  //            "len": 7
  //          }
  //        ]
  //      },
  //      {
  //        "group": [
  //          {
  //            "cap": "Name: Mary Adams",
  //            "idx": 38,
  //            "len": 16
  //          },
  //          {
  //            "cap": "Mary",
  //            "idx": 44,
  //            "len": 4
  //          },
  //          {
  //            "cap": "Adams",
  //            "idx": 49,
  //            "len": 5
  //          }
  //        ]
  //      }
  //    ]
  //  }

  //  The capture group index is obtained by looking up the name in the JSON result.
  //  For example:

  idx_first := json.IntOf('named.first');
  idx_last := json.IntOf('named.last');

  i := 0;
  matchCount := json.SizeOfArray('match');
  while i < matchCount do
    begin
      WriteLn('Match ' + i + 1 + ':');
      json.I := i;

      json.J := idx_first;
      WriteLn('first: ' + json.StringOf('match[i].group[j].cap'));

      json.J := idx_last;
      WriteLn('first: ' + json.StringOf('match[i].group[j].cap'));

      WriteLn('');
      i := i + 1;
    end;

  //  Output is: 

  //  Match 1:
  //  first: John
  //  first: Smith
  //  
  //  Match 2:
  //  first: Jack
  //  first: Johnson
  //  
  //  Match 3:
  //  first: Mary
  //  first: Adams


  sb.Free;
  json.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.