======================================================
注:本文源代码点此下载
======================================================
在 delphi 中使用正则表达式, 目前 perlregex 应该是首选, 准备彻底而细致地研究它.
官方网站: http://www.regular-expressions.info/delphi.html
直接下载: http://www.regular-expressions.info/download/tperlregex.zip
二、安装方法:
1、先把解压的 tperlregex 文件夹放一个合适的地方, 我放在了 delphi 的 imports 目录中.
2、目前最新 for win32 的版本是对 delphi 2006 的, 2007 也能用.
打开 perlregexd2006.dpk, 提示缺少资源文件, 没关系;
在 project manager 窗口中的 perlregexd2006.bpl 上点击右键, 执行 install;
这时在 tool palette 的列表中已经有了 tperlregex, 在 jgsoft 组.
3、tools -> options -> environment options -> delphi options -> library-win32 -> library path ->
添加路径: ...\imports\tperlregex
4、可以使用了! 直接 uses perlregex 或从 tool palette 添加都可以.
如果不喜欢 tool palette 的添加方式可以省略第二步.
三、使用方法
perlregex提供了tperlregex类。主要用法是:
regex : tperlregex;
....
regex := tperlregex;
try
regex.subject := '要匹配的正文';
regex.regex := '正则表达式';
if regex.match then ....
finally
regex.free
end;
如果要多次匹配并做一些处理,可以:
matched : boolean;
....
regex.match;
while regex.foundmatch do
begin
....
regex.matchagain;
end;
如果要替换匹配到的内容,可以
regex.subject := '要匹配的正文';
regex.regex := '正则表达式';
regex.replace := '替换的字符串'
if regex.match then regex.replaceall;//结果在regex.subject
或者 if regex.match then result := regex.replacement;
匹配到的字符串放在regex.matchedexpression中,长度在regex.matchedexpressionlength中,上一次匹配的结束位置在regex.stop中
匹配到的子串放在regex.subexpressions中,子串个数在regex.subexpressioncount中。
如果正则式很复杂而且常用,可创建一个生存期相对长的tperlregex实例.设置好regex属性后,使用.study方法对正则表达式进行预处理.据帮助文档说,文档资料会大大提高效率.
详情可参考文档。有一点文档上没有提到(又或者我看漏了),在第一次匹配之后,如果没有重新赋值subject,下一次匹配无论用match或者matchagain,都是从上次的结束位置开始。所以如果要重新开始匹配,应先把regex.start := 0;
四、正则单元
程序代码
unit unitregex;
interface
uses
perlregex;
function checkemail(emailaddr: string): boolean;
function checkstrornumber(str: string): boolean;
var
perlregex: tperlregex;
implementation
//email电子邮箱检测
function checkemail(emailaddr: string): boolean;
begin
perlregex := tperlregex.create(nil);
perlregex.subject := emailaddr;
perlregex.regex := '\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*';
result := perlregex.match;
end;
//字符、数字检测
function checkstrornumber(str: string): boolean;
begin
perlregex := tperlregex.create(nil);
perlregex.subject := str;
perlregex.regex := '^[a-za-z0-9]+$';
result := perlregex.match;
end;
end.
五、使用实例
程序代码
procedure tfrmregister.btnnextclick(sender: tobject);
begin
try
if not checkstrornumber(edtuser.text) then
begin
application.messagebox(pchar('请您正确输入用户名。'), '系统提示', 64);
edtuser.setfocus;
edtuser.seltext;
exit;
end
else if not checkemail(edtemail.text) then
begin
application.messagebox(pchar('请您正确输入常用的电子邮箱。'), '系统提示', 64);
edtemail.setfocus;
edtemail.seltext;
exit;
end;
except
on e: exception do
application.messagebox(pchar(e.message), '系统提示', 64)
end;
end;
六、常用正则表式
正则表达式用于字符串处理、表单验证等场合,实用高效。现将一些常用的表达式收集于此,以备不时之需。
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ascii字符计1)
匹配空白行的正则表达式:\n\s*\r
评注:可以用来删除空白行
匹配html标记的正则表达式:]*>.*?|
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
匹配首尾空白字符的正则表达式:^\s*|\s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
匹配email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
评注:表单验证时很实用
匹配网址url的正则表达式:[a-za-z]+://[^\s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-za-z][a-za-z0-9_]{4,15}$
评注:表单验证时很实用
匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
评注:匹配形式如 0511-4405222 或 021-87888822
匹配腾讯qq号:[1-9][0-9]{4,}
评注:腾讯qq号从10000开始
匹配中国邮政编码:[1-9]\d{5}(?!\d)
评注:中国邮政编码为6位数字
匹配身份证:\d{15}|\d{18}
评注:中国的身份证为15位或18位
匹配ip地址:\d+\.\d+\.\d+\.\d+
评注:提取ip地址时有用。
匹配特定数字:
^[1-9]\d*$//匹配正整数
^-[1-9]\d*$//匹配负整数
^-?[1-9]\d*$//匹配整数
^[1-9]\d*|0$//匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$//匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$//匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$//匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$//匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$//匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$//匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正。
匹配特定字符串:
^[a-za-z]+$//匹配由26个英文字母组成的字符串
^[a-z]+$//匹配由26个英文字母的大写组成的字符串
^[a-z]+$//匹配由26个英文字母的小写组成的字符串
^[a-za-z0-9]+$//匹配由数字和26个英文字母组成的字符串
^\w+$//匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式。
--------------------------------------
试验:删除http所有标签
var
reg: tperlregex;
str:string;
begin
str:=memo1.text; //有]+()\>';
while reg.matchagain do
begin
str:=stringreplace(str,reg.subexpressions[0],'',[rfreplaceall]); //删除http标签
end;
showmessage(str);
freeandnil(reg);
end;
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/