Delphi提取字符串到2个标签之间 [英] Delphi extract string between to 2 tags
问题描述
如何使用delphi在2个html标签之间提取文本?
这是一个示例字符串。
How would I go about extracting text between 2 html tags using delphi? Here is an example string.
blah blah blah<tag>text I want to keep</tag>blah blah blah
,我想提取这部分内容。
and I want to extract this part of it.
<tag>text I want to keep</tag>
(基本上删除了之前和之后的所有blah blah blah垃圾, < tag>
& < / tag>
我还想保留的字符串。
(basically removing all the blah blah blah garbage that comes before and after the <tag>
& </tag>
strings which I also want to keep.
像我说的那样,我相信这对于那些知道的人来说是非常容易的,但是我现在不能把头靠近,谢谢你的回复。
Like I said, I am sure this is extremely easy for those who know, but I just cannot wrap my head around it at the moment. Thanks in advance for your replies.
推荐答案
这完全取决于您的输入内容。
This depends entirely on how your input looks.
更新一些特殊情况的解决方案,但在OP解释了一些细节之后,我不得不对它们进行一般化,这里是最一般的代码:
Update First I wrote a few solutions for special cases, but after the OP explained a bit more about the details, I had to generalize them a bit. Here is the most general code:
function ExtractTextInsideGivenTagEx(const Tag, Text: string): string;
var
StartPos1, StartPos2, EndPos: integer;
i: Integer;
begin
result := '';
StartPos1 := Pos('<' + Tag, Text);
EndPos := Pos('</' + Tag + '>', Text);
StartPos2 := 0;
for i := StartPos1 + length(Tag) + 1 to EndPos do
if Text[i] = '>' then
begin
StartPos2 := i + 1;
break;
end;
if (StartPos2 > 0) and (EndPos > StartPos2) then
result := Copy(Text, StartPos2, EndPos - StartPos2);
end;
function ExtractTagAndTextInsideGivenTagEx(const Tag, Text: string): string;
var
StartPos, EndPos: integer;
begin
result := '';
StartPos := Pos('<' + Tag, Text);
EndPos := Pos('</' + Tag + '>', Text);
if (StartPos > 0) and (EndPos > StartPos) then
result := Copy(Text, StartPos, EndPos - StartPos + length(Tag) + 3);
end;
样本用法
Sample usage
ExtractTextInsideGivenTagEx('tag',
'blah <i>blah</i> <b>blah<tag a="2" b="4">text I want to keep</tag>blah blah </b>blah')
返回
text I want to keep
而
ExtractTagAndTextInsideGivenTagEx('tag',
'blah <i>blah</i> <b>blah<tag a="2" b="4">text I want to keep</tag>blah blah </b>blah')
返回
<tag a="2" b="4">text I want to keep</tag>
这篇关于Delphi提取字符串到2个标签之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!