implementation
uses
Pcre;
function textReplaceHtmlTagToPounc(const Atext:AnsiString):AnsiString;
const
SHtmlTagRegEx='&/w+;|&#/d+;';
var
tt:TStringList;
i:Integer;
Regex : IRegex;//声明正则类型
MatchCollection:IMatchCollection;//声明结果容器
match:IMatch;//声明单一结果
begin
Result:=Atext;
if Result='' then Exit;
if not (Pos('&',Result)>0) then Exit;
tt:=TStringList.Create;
try
regex:=RegexCreate(SHtmlTagRegEx,[]);//创建
MatchCollection:=Regex.Matches(Atext);//进行匹配
if MatchCollection.Count>0 then
for i := 0 to MatchCollection.Count - 1 do
begin
match:=MatchCollection[i];//得到其中的结果
tt.Add(match.Value);//读取结果中的匹配字符串
end;
if tt.Count > 0 then
Result:=HtmlTagToPounc(Result,tt);
finally
tt.Free;
end;
end;
textReplaceHtmlTagToPounc函数完成功能:
1.从Atext中查询HtmlTag标记,如果查询到标记,则存入 tt中
2.如果tt不为空,则使用HtmlTagToPounc将查询到的HtmlTag替换为Ansi字符
本文介绍了一个用于替换HTML标签的函数textReplaceHtmlTagToPounc。该函数通过正则表达式匹配HTML标签,并将其转换为Ansi字符。适用于需要清理HTML标签并转换为纯文本的应用场景。
3645

被折叠的 条评论
为什么被折叠?



