How to check if a string contains only letters, numbers, underscores and dashes in Pascal

1 Answer

0 votes
program RegexStringValidation;

uses
  RegExpr;

function IsValidString(s: string): Boolean;
var
  Regex: TRegExpr;
begin
  Regex := TRegExpr.Create;
  Regex.Expression := '^[A-Za-z0-9_-]*$';
  IsValidString := Regex.Exec(s);
  Regex.Free;
end;

begin
  if IsValidString('-abc_123-') then
    Writeln('yes')
  else
    Writeln('no');

  if IsValidString('-abc_123-(!)') then
    Writeln('yes')
  else
    Writeln('no');
end.

   
     
(*
run:
 
yes
no
     
*)

 



answered May 31, 2025 by avibootz
...