How to declare a RegEx with character repetition to match the strings "http", "htttp", "httttp", etc in Pascal

1 Answer

0 votes
program RegexMatch;

uses
  RegExpr;

var
  Regex: TRegExpr;
  TestStrings: array[0..4] of string = ('http', 'htttp', 'httttp', 'httpp', 'htp');
  i: Integer;

begin
  // Initialize regex pattern
  Regex := TRegExpr.Create;
  
  Regex.Expression := 'htt+p'; // Equivalent to Python's r"htt+p"

  // Check matches
  for i := Low(TestStrings) to High(TestStrings) do
  begin
    if Regex.Exec(TestStrings[i]) then
      WriteLn('Matches "', TestStrings[i], '": true')
    else
      WriteLn('Matches "', TestStrings[i], '": false');
  end;
  
  Regex.Free; // Free memory
end.


// Matches "httpp": True or false, depending on how matches() method works

  
(*
run:
  
Matches "http": true
Matches "htttp": true
Matches "httttp": true
Matches "httpp": true
Matches "htp": false
  
*)  



 



answered May 15, 2025 by avibootz
...