正则表达式(转自http://blogold.chinaunix.net/u/941/showart_1742800.html)
来源:互联网 发布:php获取get的所有参数 编辑:程序博客网 时间:2024/04/30 21:12
正则表达式的用法(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 的添加方式可以省略第二步.
计划的学习步骤: 1、正则语法; 2、TPerlRegEx 功能.
字符替换
begin
with PerlRegEx1 do begin
RegEx:=Edit1.Text;
Subject:=Edit2.Text; //匹配属生的RegEx与Subject
if Match then ShowMessage('Match') else ShowMessage('Not Match');
end;
end;
字串替换的用法
------------------------
with PerlRegEx1 do begin
Subject := 'Bill loves Hillary';
RegEx := 'Hillary';
Replacement := 'Monica';
if Match then Replace;
ShowMessage(Subject); // "Bill loves Monica"
end;
-------------------------------------------------
任意匹配
----------------
with PerlRegEx1 do begin
Subject:='linux';
RegEx := 'Windows|Linux'; // Matches 'Windows' or 'Linux', whichever comes first
if Match then Label1.Caption := MatchedExpression + ' rulez!';
end;
-------------------
Option->CaseLess->True->不区分大小写,否则区分大小写
_____________________________________________________
字符匹配
-------------
with PerlRegEx1 do begin
Subject := 'Rating: AAA';
RegEx := 'A+'; // Matches the first occurence of 'A', including any A's that may follow right after it
if Match then Label1.Caption :=
'You have been rated with ' + IntToStr(MatchedExpressionLength) + 'A''s'
else
Label1.Caption := 'You have a poor rating';
end;
只要出现A就匹配成功!
______________________________
取得匹配字串前面的字串,当也可以取得后面的字串
----------------------------------
with PerlRegEx1 do begin
Subject:='HaHa Bye Hei Hei';
RegEx := 'Bye';
if Match then Label1.Caption := 'Before typing "Bye", you typed: ' +
Copy(Subject, 1, MatchedExpressionOffset-1);
end;
--------------------------------
第一个字符为大写的匹配方式
---------------------------------
---------------------------------加如文本------
One,Two,Three,Four
你, 我, 他
你们, 我们, 他们
----------------------------------------------
procedure TForm1.Button2Click(Sender: TObject);
begin
PerlRegEx1.Subject:=Memo1.Lines.GetText;
PerlRegEx1.RegEx:=',';
PerlRegEx1.Split(Memo2.Lines,0);
end;
-----裂开后,每个子字串都占一行-------------
One
Two
Three
Four
你
我
他
你们
我们
他们
----------代码如下-----------------
begin
PerlRegEx1.Subject:=Memo1.Lines.GetText;
PerlRegEx1.RegEx:=',';
PerlRegEx1.Split(Memo2.Lines,0);//0表示源字串中的所有子字串
end;
----------------------------------
这个功能在做文本数据库时最好用->把文本裂开后存入数据表中
直接下载: 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 的添加方式可以省略第二步.
计划的学习步骤: 1、正则语法; 2、TPerlRegEx 功能.
字符替换
//先测试一下:
uses
PerlRegEx; //uses 正则表达式单元
procedure TForm1.FormCreate(Sender: TObject);
var
reg: TPerlRegEx; //声明正则表达式变量
begin
reg := TPerlRegEx.Create(nil); //建立
reg.Subject := 'sSsS'; //这是要替换的源字符串
reg.RegEx := 's'; //这是表达式, 在这里是准备替换掉的子串
reg.Replacement := '◆'; //要替换成的新串
reg.ReplaceAll; //执行全部替换
ShowMessage(reg.Subject); //返回替换结果: ◆S◆S
FreeAndNil(reg); //因为建立时属主给了 nil, 这里没有使用 reg.Free
end;
字串的绝对匹配用法
procedure TForm1.Button1Click(Sender: TObject);
begin
with PerlRegEx1 do begin
RegEx:=Edit1.Text;
Subject:=Edit2.Text; //匹配属生的RegEx与Subject
if Match then ShowMessage('Match') else ShowMessage('Not Match');
end;
end;
字串替换的用法
------------------------
with PerlRegEx1 do begin
Subject := 'Bill loves Hillary';
RegEx := 'Hillary';
Replacement := 'Monica';
if Match then Replace;
ShowMessage(Subject); // "Bill loves Monica"
end;
-------------------------------------------------
任意匹配
----------------
with PerlRegEx1 do begin
Subject:='linux';
RegEx := 'Windows|Linux'; // Matches 'Windows' or 'Linux', whichever comes first
if Match then Label1.Caption := MatchedExpression + ' rulez!';
end;
-------------------
Option->CaseLess->True->不区分大小写,否则区分大小写
_____________________________________________________
字符匹配
-------------
with PerlRegEx1 do begin
Subject := 'Rating: AAA';
RegEx := 'A+'; // Matches the first occurence of 'A', including any A's that may follow right after it
if Match then Label1.Caption :=
'You have been rated with ' + IntToStr(MatchedExpressionLength) + 'A''s'
else
Label1.Caption := 'You have a poor rating';
end;
只要出现A就匹配成功!
______________________________
取得匹配字串前面的字串,当也可以取得后面的字串
----------------------------------
with PerlRegEx1 do begin
Subject:='HaHa Bye Hei Hei';
RegEx := 'Bye';
if Match then Label1.Caption := 'Before typing "Bye", you typed: ' +
Copy(Subject, 1, MatchedExpressionOffset-1);
end;
--------------------------------
第一个字符为大写的匹配方式
---------------------------------
with PerlRegEx1 do begin
Subject := 'I like to hang out at Foo bar';
RegEx := '([A-Za-z]+) bar';//([A-Za-z]+) 这个表示第一个字符大写,后面不管
Replacement := ' /1 is the name of the bar I like';
if Match then Replace;
ShowMessage(Subject);
end;
分裂字串(把一个字串按指定分隔号裂开)
---------------------------------加如文本------
One,Two,Three,Four
你, 我, 他
你们, 我们, 他们
----------------------------------------------
procedure TForm1.Button2Click(Sender: TObject);
begin
PerlRegEx1.Subject:=Memo1.Lines.GetText;
PerlRegEx1.RegEx:=',';
PerlRegEx1.Split(Memo2.Lines,0);
end;
-----裂开后,每个子字串都占一行-------------
One
Two
Three
Four
你
我
他
你们
我们
他们
----------代码如下-----------------
begin
PerlRegEx1.Subject:=Memo1.Lines.GetText;
PerlRegEx1.RegEx:=',';
PerlRegEx1.Split(Memo2.Lines,0);//0表示源字串中的所有子字串
end;
----------------------------------
这个功能在做文本数据库时最好用->把文本裂开后存入数据表中
--本文转自月夜我飞翔的博客:http://blogold.chinaunix.net/u/941/showart_1742800.html
- 正则表达式(转自http://blogold.chinaunix.net/u/941/showart_1742800.html)
- C++ Primer学习笔记——$19 多重继承与虚继承(转自:http://blogold.chinaunix.net/u/18517/showart_252162.html)
- http://blogold.chinaunix.net/u3/109648/showart_2159438.html
- http://blogold.chinaunix.net/u1/43765/showart_482530.html
- perl特殊变量 转帖:http://blogold.chinaunix.net/u/8985/article.php? frmid=19739
- XPath介绍(转自http://blog.chinaunix.net/u/24129/showart_311035.html)
- Qemu 中文手册 (转自:http://blog.chinaunix.net/u/12679/showart_432398.html)
- 长短字符串问题(转载自http://blog.chinaunix.net/u/25675/showart_261303.html)
- sybase笔记 转自:http://blog.chinaunix.net/u/12390/showart.php?id=473402
- fckeditor的用法(转)里面有具体例子(http://blog.chinaunix.net/u/16215/showart_214196.html)
- 数据库的静默和挂起状态(http://blog.chinaunix.net/u/22151/showart_1165163.html)
- Linux Socket学习 (转自 http://blog.chinaunix.net/uid/13670711/frmd/-1/page/7.html)
- 解释器与编译器 (本文转自:http://blog.chinaunix.net/uid-22363424-id-113275.html)
- http://blogold.chinaunix.net/u3/90973/showart.php?id=2061985
- pthread 不错的总结(转自http://blog.chinaunix.net/u1/35045/showart_396435.html)
- Java中各进制数之间的转换[转自http://blog.chinaunix.net/u2/62809/showart_515317.html]
- QT Embedded二三事之QEventLoop(转自http://blog.chinaunix.net/u3/92787/showart_1946832.html)
- LibCurl编程 【转自http://blog.chinaunix.net/u1/57905/showart_1950598.html】
- MFC做的魔兽改建精灵
- /etc/inittab文件的字段及其说明
- Java开源库
- MFC做的基于socket聊天工具
- 我的RamAsm颜色设置
- 正则表达式(转自http://blogold.chinaunix.net/u/941/showart_1742800.html)
- [转载]手机号码IP地址归属地查询
- 用DOS命令创建用户并将用户归属相应组
- myeclipse配置tomcat环境
- linux下rsync的使用
- [转]C++异常中的堆栈跟踪
- linux常用命令,做个标记
- Web开发人员必备的20款超赞的jQuery插件
- IF THEN用法