正 文:
使用Delphi开发软件无疑是一种比较高效率的方式,完全对象的语言,严格控制的语法,可 视化开发环境的性能,编译器的速度和已编译代码的效率,编程语言的功能及其复杂性,数据库结构的灵活性和可扩展性,框架对设计和使用模式的扩充无疑都是 delphi身上的光芒。 最近飘易使用Delphi开发了一套工具,由于和网络应用相关的东西较多,需要使用正则表达式,而目前主流的delphi下的正则表达式,应该 是 PerlRegEx 。 官方网站: http://www.regular-expressions.info/delphi.html
直 接下载: http://www.regular-expressions.info/download/TPerlRegEx.zip 安装方法(飘易使用的delphi版本是 delphi2007): 1、先把解压的 TPerlRegEx 文件夹放在 Delphi 的根目录下的 Imports 目录中。 2、目前最新 For Win32 的版本是可以在 Delphi7,Delphi 2006,Delphi 2007,Delphi 2009 下使 用。飘易用的是2007,于是就打开 PerlRegExD2007.dpk ,在 Project Manager 窗口中 的 PerlRegExD2007.bpl 上点击右键,执行 Install。这时在 Tool Palette 的列表中已经有 了 TPerlRegEx,在 JGsoft 组。 3、 Tools -> Options -> Environment Options -> Delphi Options -> Library- Win32 -> Library path -> 添加路径:$(BDS)\Imports\TPerlRegEx (注:就是刚才放置的 路径)。 4、打开delphi根目录下 Imports\TPerlRegEx\pcre ,把 pcre3.dll 文件复制到 C:\WINDOWS \system32 文件夹下,这个文件夹里是windows系统存放 dll 文件的大本营。如果不复制pcre3.dll,在应 用 TPerlRegEx 正则的时候,系统提示出错。 5、用delphi打开 Imports\TPerlRegEx 目录下的 pcre.pas 文件,找 到 //{$DEFINE PCRE_STATICLINK} ,把之前的注释 // 删除;找到 {$DEFINE PCRE_LINKDLL} ,把 它注释掉,即改为 //{$DEFINE PCRE_LINKDLL} 。 注意:这点非常重要,如果不这样改的话,在软件开发完成后,分发到其他电脑上的时候,软件就会提示“缺少pcre3.dll错误”。这样修改的原因,是把 正则直接封包到 exe 文件里,而不需要链接 pcre3.dll 了;封包进 exe,付出的代价是软件增大了 100KB 左右的大小,当然比起必 须携带 pcre3.dll来说,这是有意义的付出。 具体的原因,看下面的官方解释:
// Define PCRE_STATICLINK to link the OBJ files with PCRE 7.7.
// Due to bugs in the Delphi compiler, you may get an internal error if install TPerlRegEx into a design time package,
// and you don't put TPerlRegEx into a runtime package either.
// You can use PCRE_STATICLINK if you don't use packages at all (which means you don't install it into the IDE and you don't drop TPerlRegEx on a form)
// You can also use PCRE_STATICLINK if you put the component into a runtime package
6、可以使用了! 直接 uses PerlRegEx 或从 Tool Palette 添加都可以。 (以上部分方法参考) TPerlRegEx 安装设置完成后,就可以舒心地使用了,一些简单的使用方法如下: // Due to bugs in the Delphi compiler, you may get an internal error if install TPerlRegEx into a design time package,
// and you don't put TPerlRegEx into a runtime package either.
// You can use PCRE_STATICLINK if you don't use packages at all (which means you don't install it into the IDE and you don't drop TPerlRegEx on a form)
// You can also use PCRE_STATICLINK if you put the component into a runtime package
//查找是否存在
var
reg: TPerlRegEx;
begin
reg := TPerlRegEx.Create(nil);
reg.Subject := 'CodeGear Delphi 2007 for Win32';
reg.RegEx := '\d';
if reg.Match then
ShowMessage(' 找到了')
else
ShowMessage('没找到');
FreeAndNil(reg);
end;
var
reg: TPerlRegEx;
begin
reg := TPerlRegEx.Create(nil);
reg.Subject := 'CodeGear Delphi 2007 for Win32';
reg.RegEx := '\d';
if reg.Match then
ShowMessage(' 找到了')
else
ShowMessage('没找到');
FreeAndNil(reg);
end;
//替换一般字符串
var
reg: TPerlRegEx;
begin
reg := TPerlRegEx.Create(nil);
reg.Subject := ' 我爱DELPHI, 但Delphi不爱我!';
reg.RegEx := 'Delphi';
reg.Replacement := '◆';
reg.ReplaceAll;
ShowMessage(reg.Subject); // 返回: 我爱DELPHI, 但◆不爱我!
FreeAndNil(reg);
end;
var
reg: TPerlRegEx;
begin
reg := TPerlRegEx.Create(nil);
reg.Subject := ' 我爱DELPHI, 但Delphi不爱我!';
reg.RegEx := 'Delphi';
reg.Replacement := '◆';
reg.ReplaceAll;
ShowMessage(reg.Subject); // 返回: 我爱DELPHI, 但◆不爱我!
FreeAndNil(reg);
end;
//分别显示找到的每一个和总数
var
reg: TPerlRegEx;
num: Integer; // 用 num 来计数
begin
reg := TPerlRegEx.Create(nil); reg.Subject := 'CodeGear Delphi 2007 for Win32';
reg.RegEx := '\d'; num := 0;
while reg.MatchAgain do //MatchAgain 是下一个
begin
ShowMessage(reg.MatchedExpression); // 将分别显示: 2 0 0 7 3 2
Inc(num);
end;
ShowMessage(IntToStr(num)); //6 FreeAndNil(reg);
end;
//目标字符串的位置与长度var
reg: TPerlRegEx;
num: Integer; // 用 num 来计数
begin
reg := TPerlRegEx.Create(nil); reg.Subject := 'CodeGear Delphi 2007 for Win32';
reg.RegEx := '\d'; num := 0;
while reg.MatchAgain do //MatchAgain 是下一个
begin
ShowMessage(reg.MatchedExpression); // 将分别显示: 2 0 0 7 3 2
Inc(num);
end;
ShowMessage(IntToStr(num)); //6 FreeAndNil(reg);
end;
var
reg: TPerlRegEx;
begin
reg := TPerlRegEx.Create(nil); reg.Subject := 'CodeGear Delphi 2007 for Win32';
reg.RegEx := 'Delphi'; while reg.MatchAgain do //很明显: 本例只能找到一个结果
begin
ShowMessage(reg.MatchedExpression); // 找到的字符串: Delphi
ShowMessage(IntToStr(reg.MatchedExpressionOffset)); // 它所在的位置: 10
ShowMessage(IntToStr(reg.MatchedExpressionLength)); // 它的长度: 6
end; FreeAndNil(reg);
end;
没有评论:
发表评论