我有一个很大的字符串......原始文本是这样的:
'Lorem ipsum dolor sit amet, consectetur adipiscing <a href="hxxp://www.youtube.com/watch?v=VIDEO_1">hxxp://www.youtube.com/watch?v=VIDEO_1</a>
Sed lacinia purus turpis. Curabitur in nisi urna, vitae aliquet
Vestibulum ante ipsum primis in faucibus orci luctus hxxp://www.youtube.com/watch?v=VIDEO_2</a>'
如果您发现某个视频 (VIDEO_2) 的结尾为 而没有打开 这些有问题的视频可能位于原始文本中的任意位置和任意数量。
我想删除那些不必要的 我如何检测并删除它们?
我使用的是 Delphi XE4。 请问有什么帮助吗?
最佳答案
我相信以下代码可以有效地工作:
function RemoveLonelyClosingATags(const S: string): string;
var
level: integer;
i: Integer;
ActualLength: integer;
begin
level := 0;
SetLength(result, Length(S));
ActualLength := 0;
i := 1;
while i <= Length(S) do
begin
if (S[i] = '<') and (UpperCase(Copy(S, i, 4)) = '</A>') then
begin
if Level = 0 then
begin
inc(i, 4);
Continue;
end
else
dec(Level);
end;
inc(ActualLength);
result[ActualLength] := S[i];
if (S[i] = '<') and (i < Length(S)) and (UpperCase(S[i+1]) = 'A') then
begin
inc(Level);
if Level > 1 then
raise Exception.Create('Nested A tags detected.');
end;
inc(i);
end;
SetLength(result, ActualLength);
end;
关于delphi - 查找并删除字符串的模式(Delphi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759271/