delphi正则应用
2016-07-21 14:29
537 查看
function TrackMatchs(strText: string;
strExp : string;
Matchs : TStrings=nil;
bMS : Boolean=false;
bMG : Boolean=false;
bMR : Boolean=false): integer;
var
i: integer;
RegExpr: TRegExpr;
begin
result:= 0;
if strExp='' then exit;
RegExpr:= TRegExpr.Create;
RegExpr.ModifierS:= bMS;
RegExpr.ModifierG:= bMG;
RegExpr.ModifierR:= bMR;
RegExpr.ModifierI:= true; //不区分大小写
RegExpr.Expression:= strExp;
try
RegExpr.Expression := strExp;
if RegExpr.Exec (strText) then
begin
REPEAT
if Assigned(Matchs) then
Matchs.Add(RegExpr.Match[0]);
Inc(result);
UNTIL not RegExpr.ExecNext;
end;
finally
FreeAndNil(RegExpr);
end;
end;
function GetMatchs(strText: string;
strExp : string;
Matchs : TStrings=nil;
bMS : Boolean=true;
bMG : Boolean=false;
bMR : Boolean=false;
Ver : byte=1;
iContent: integer=0): integer;
//function GetMatchContent(strText: widestring; iContent, iMatchPos: integer): string;
function GetMatchContent(strText: string; iContent, iMatchPos: integer; var line: integer): string;
var
v: string;
istart, iend: integer;
begin
istart:= iMatchPos-(iContent div 2);
if istart<1 then istart:=1;
//iend:= iMatchPos+(iContent div 2);
//if iend>length(strText) then iend:=length(strText);
result:= copy(strText, istart, iContent);
//2014-11-24 17:14 tig 输出所在行
v:= copy(strText, 1, istart);
line:= StrCount(#13#10, v);
{
result:= '';
if iContent<length(strMatch) then exit;
ipos:= pos(strMatch, strText);
if ipos<1 then exit;
istart:= ipos-(iContent div 2);
if istart<1 then istart:=1;
iend:= ipos+(iContent div 2);
if iend>length(strText) then iend:=length(strText);
result:= copy(strText, istart, iend-istart);
}
end;
var
RegExpr: TRegExpr;
i, nums, line: integer;
sline, cont: string;
DataStrs: TStrings;
begin
result:= 0;
if strExp='' then exit;
RegExpr := TRegExpr.Create;
DataStrs := TStringList.Create;
try
with RegExpr do
begin
ModifierS := bMS;
ModifierG := bMG;
ModifierR := bMR;
ModifierI := true; //不区分大小写
Expression := strExp;
if Exec(strText) then
begin
REPEAT
sline:= '';
if (Ver=2) or (Ver=3) then
begin
nums:=0;
if Ver=2 then nums:= RegExpr.SubExprMatchCount;
cont:= '';
line:= -1;
for i:= 0 to nums do
begin
if iContent>0 then
begin
cont:= GetMatchContent(strText, iContent, RegExpr.MatchPos[i], line);
cont:=FastReplace(cont, #13#10, '', false);
cont:=FastReplace(cont, #09, '', false);
end;
sline:= sline+'<line>'+inttostr(line)+'</line>'+
'<match>'+RegExpr.Match[i]+'</match>'+
'<content>'+cont+'</content>'+#09
end;
end else
begin
sline:= RegExpr.Match[iContent];
end;
Inc(result);
DataStrs.Add(sline);
UNTIL not ExecNext;
if Assigned(Matchs) then Matchs.Assign(DataStrs);
end;
end;
finally
RegExpr.Free;
DataStrs.Free;
end;
end;
function GetMatch(strText: string;
strExp : string;
idx : integer=0; //0表示
bMS : Boolean=true;
bMG : Boolean=false;
bMR : Boolean=false): string;
var
i, nums: integer;
RegExpr: TRegExpr;
strLine, strContent: string;
begin
result:= '';
if strExp='' then exit;
RegExpr:= TRegExpr.Create;
RegExpr.ModifierS:= bMS;
RegExpr.ModifierG:= bMG;
RegExpr.ModifierR:= bMR;
RegExpr.ModifierI:= true; //不区分大小写
RegExpr.Expression:= strExp;
RegExpr.Expression:= strExp;
if RegExpr.Exec (strText) then
begin
if idx>RegExpr.SubExprMatchCount then idx:= 0;
result:= RegExpr.Match[idx];
end;
RegExpr.Free;
end;
strExp : string;
Matchs : TStrings=nil;
bMS : Boolean=false;
bMG : Boolean=false;
bMR : Boolean=false): integer;
var
i: integer;
RegExpr: TRegExpr;
begin
result:= 0;
if strExp='' then exit;
RegExpr:= TRegExpr.Create;
RegExpr.ModifierS:= bMS;
RegExpr.ModifierG:= bMG;
RegExpr.ModifierR:= bMR;
RegExpr.ModifierI:= true; //不区分大小写
RegExpr.Expression:= strExp;
try
RegExpr.Expression := strExp;
if RegExpr.Exec (strText) then
begin
REPEAT
if Assigned(Matchs) then
Matchs.Add(RegExpr.Match[0]);
Inc(result);
UNTIL not RegExpr.ExecNext;
end;
finally
FreeAndNil(RegExpr);
end;
end;
function GetMatchs(strText: string;
strExp : string;
Matchs : TStrings=nil;
bMS : Boolean=true;
bMG : Boolean=false;
bMR : Boolean=false;
Ver : byte=1;
iContent: integer=0): integer;
//function GetMatchContent(strText: widestring; iContent, iMatchPos: integer): string;
function GetMatchContent(strText: string; iContent, iMatchPos: integer; var line: integer): string;
var
v: string;
istart, iend: integer;
begin
istart:= iMatchPos-(iContent div 2);
if istart<1 then istart:=1;
//iend:= iMatchPos+(iContent div 2);
//if iend>length(strText) then iend:=length(strText);
result:= copy(strText, istart, iContent);
//2014-11-24 17:14 tig 输出所在行
v:= copy(strText, 1, istart);
line:= StrCount(#13#10, v);
{
result:= '';
if iContent<length(strMatch) then exit;
ipos:= pos(strMatch, strText);
if ipos<1 then exit;
istart:= ipos-(iContent div 2);
if istart<1 then istart:=1;
iend:= ipos+(iContent div 2);
if iend>length(strText) then iend:=length(strText);
result:= copy(strText, istart, iend-istart);
}
end;
var
RegExpr: TRegExpr;
i, nums, line: integer;
sline, cont: string;
DataStrs: TStrings;
begin
result:= 0;
if strExp='' then exit;
RegExpr := TRegExpr.Create;
DataStrs := TStringList.Create;
try
with RegExpr do
begin
ModifierS := bMS;
ModifierG := bMG;
ModifierR := bMR;
ModifierI := true; //不区分大小写
Expression := strExp;
if Exec(strText) then
begin
REPEAT
sline:= '';
if (Ver=2) or (Ver=3) then
begin
nums:=0;
if Ver=2 then nums:= RegExpr.SubExprMatchCount;
cont:= '';
line:= -1;
for i:= 0 to nums do
begin
if iContent>0 then
begin
cont:= GetMatchContent(strText, iContent, RegExpr.MatchPos[i], line);
cont:=FastReplace(cont, #13#10, '', false);
cont:=FastReplace(cont, #09, '', false);
end;
sline:= sline+'<line>'+inttostr(line)+'</line>'+
'<match>'+RegExpr.Match[i]+'</match>'+
'<content>'+cont+'</content>'+#09
end;
end else
begin
sline:= RegExpr.Match[iContent];
end;
Inc(result);
DataStrs.Add(sline);
UNTIL not ExecNext;
if Assigned(Matchs) then Matchs.Assign(DataStrs);
end;
end;
finally
RegExpr.Free;
DataStrs.Free;
end;
end;
function GetMatch(strText: string;
strExp : string;
idx : integer=0; //0表示
bMS : Boolean=true;
bMG : Boolean=false;
bMR : Boolean=false): string;
var
i, nums: integer;
RegExpr: TRegExpr;
strLine, strContent: string;
begin
result:= '';
if strExp='' then exit;
RegExpr:= TRegExpr.Create;
RegExpr.ModifierS:= bMS;
RegExpr.ModifierG:= bMG;
RegExpr.ModifierR:= bMR;
RegExpr.ModifierI:= true; //不区分大小写
RegExpr.Expression:= strExp;
RegExpr.Expression:= strExp;
if RegExpr.Exec (strText) then
begin
if idx>RegExpr.SubExprMatchCount then idx:= 0;
result:= RegExpr.Match[idx];
end;
RegExpr.Free;
end;
相关文章推荐
- Java简单字符串插值实现
- C# 正则判断一个数字的格式是否有逗号的代码
- C#常用正则大全分享
- 实例详解C#正则表达式
- php正则替换处理HTML页面的方法
- php的正则处理函数总结分析
- 正则方式的自动小偷抓网程序
- asp 去掉html中的table正则代码函数
- mysqlhotcopy 正则使用小技巧
- js下函数般调用正则的方法附代码
- php实现使用正则将文本中的网址转换成链接标签
- php下正则来匹配dede模板标签的代码
- asp 验证输入网址是否有效并可以访问 与正则验证输入网址
- php 正则 过滤html 的超链接
- JS正则中的RegExp对象对象
- 正则表达式语法
- .NET读取所有目录下文件正则匹配文本电子邮件
- 常用正则 常用的C#正则表达式
- PHP一些常用的正则表达式字符的一些转换
- 去除段首段尾的 和全角的空格的正则