且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

freepascal正则表达式替换

更新时间:2022-11-12 09:49:58

据我所知,PerlRegEx单元与Free Pascal不兼容.但是您可以使用Free Pascal随附的RegExpr单元.

As far as I know, the PerlRegEx unit isn't compatible with Free Pascal. But you can use the RegExpr unit, which comes with Free Pascal.

如果我的理解正确,您想用替代词代替.这是一个简单的示例,您可以根据需要进行调整.

If I understand correctly, you want a replacement with substitution. Here is a simple example that you can adapt to your need.

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

uses
  regexpr;

var
  s: string;

begin
  s := 'My name is Bond.';

  s := ReplaceRegExpr(
    'My name is (\w+?)\.',
    s,
    'His name is $1.',
    TRUE // Use substitution
  );

  WriteLn(s); // His name is Bond.
  ReadLn;
end.