Delphi 正则表达式 TPerlRegEx 类
2013-10-30 16:52
351 查看
抄自:万一的博客 http://www.cnblogs.com/del/category/113551.html
目录:
基本方法
查找(目标字符串及其属性)
字表达式
限定匹配范围:start、stop
字符串分隔
基本方法
查找
子表达式
限定范围:start、stop
字符串分隔
目录:
基本方法
查找(目标字符串及其属性)
字表达式
限定匹配范围:start、stop
字符串分隔
基本方法
use PerlRegEx; //引用单元 var reg:TPerlRegEx; //声明变量类型 begin reg:= TPerlRegEx.Create; //创建实例 reg.Subject:= 'Goodbye Hello-World !'; //源字符串 reg.RegEx:= '(?<=Goodbye ).*'; //正规表达式 reg.Match; //执行查找 ShowMessage(reg.MatchedText); //输出查找结果 FreeAndNil(reg); //释放内存 end;
查找
//查看是否存在 if reg.Match then ... or begin reg.Match; if reg.FoundMatch then ... end
//显示找到的第一个 if reg.Match then ShowMessage(reg.MatchedText)
//分别显示找到的每一个 while reg.MatchAgain do //MatchAgain 是下一个 begin ShowMessage(reg.MatchedText); Inc(num); end;
//目标字符串的 位置 和 长度 while reg.MatchAgain do begin ShowMessage(reg.MatchedText); //找到的字符串 ShowMessage(IntToStr(reg.MatchedOffset));//位置 ShowMessage(IntToStr(reg.MatchedLength));//长度 end;
子表达式
//字表达式:Groups[Index :Integer]:String; reg.Subject:= 'Goodbye Hello-World !'; reg.RegEx:= '(?<=Goodbye )(\w*)-?(\w*)'; reg.Match; for i:= 0 to reg.GroupCount do begin ShowMessage(reg.Groups[i]); end; //分别显示“Hello-World”,“Hello”,“World”
限定范围:start、stop
//使用不可以使用ReplaceAll ,否则仍会替换全部内容 reg.Subject := 'ababab'; reg.Start := 5; reg.Stop := 6; while reg.MatchAgain do begin reg.Replace; end; ShowMessage(reg.Subject); //返回: abab◆
字符串分隔
reg.Subject := 'aaa,bbb,ccc,ddd'; reg.RegEx := ','; //确定字符串的分隔符 reg.Split(List,MaxInt); //List: TStrings; 用于存放分割后的字符串 { 输入一个最大整数, 表示能分多少就分多少} ShowMessage(List.Text); {返回: aaa bbb ccc ddd }
相关文章推荐
- delphi7调用java写的webservice,在调用的时候弹出“wssecurityhandler:request does not contain required security header”
- delphi7调用webservice Java 传入参数为空
- delphi客户端调服务器端的java webservice如何在参数中传对象? 转
- DELPHI7如何调用带参数的JAVA WebService
- Delphi调用java开发的WebService,传入参数出错
- *,vol.Delphi.Setup.Hash.MD5
- ,vol.Delphi.Setup
- delphi 线程
- Delphi 的运算符列表
- delphi 线程的使用
- Delphi下遍历文件夹下所有文件的递归算法
- delphi 2010安装unidac
- Delphi从Internet下载文件
- delphi 理解ParamStr
- delphi2010多线程编程教程
- QQ2008自动聊天精灵delphi源码
- Delphi使用Indy、ICS组件读取网页
- delphi程序全局快捷键
- Delphi 正则表达式
- AV 地址错误 map 文件 根据地址报错,查 Delphi 代码