Delphi 如何从一组键=值对中获取电子邮件地址?
我怎样才能得到我需要的部分字符串 accountid=xxxxxx type=prem servertime=1256876305 addtime=1185548735 validuntil=1265012019 username=noob directstart=1 protectfiles=0 rsantichack=1 plustrafficmode=1 mirrors=jsconfig=1 email=noob@live.comlots=0 fpoints=6076 ppoints=149 curfiles=38 curspace=3100655714 bodkb=60000000 premkbleet=25000000 ppointrate=116Delphi 如何从一组键=值对中获取电子邮件地址?,delphi,delphi-2007,string-parsing,Delphi,Delphi 2007,String Parsing,我怎样才能得到我需要的部分字符串 accountid=xxxxxx type=prem servertime=1256876305 addtime=1185548735 validuntil=1265012019 username=noob directstart=1 protectfiles=0 rsantichack=1 plustrafficmode=1 mirrors=jsconfig=1 email=noob@live.comlots=0 fpoints=6076 ppoints=14
我想要email=之后的数据,但最多只能访问live.com.?以下代码仅在值不包含空格时有效:
uses
StrUtils, Classes;
....
function GetPropertyValue (const PropertyName : String; const InputString : String) : String;
var
StringList : TStringList;
Str : String;
begin
Result := '';
StringList := TStringList.Create;
try
StringList.Delimiter := ' ';
StringList.DelimitedText := InputString;
for Str in StringList do
if StartsText (PropertyName + '=', Str) then
Result := RightStr (Str, Length (Str) - Length (PropertyName) - 1);
finally
FreeAndNil (StringList);
end;
end;
我的想法是:
有几种方法可以做到这一点。您可以拆分空格字符上的字符串,然后将其馈送到TStringList中。然后,您可以使用TStringList的Value[String]属性来获取给定名称的值 为此,请使用逗号替换所有空格的字符串:
newString := StringReplace(oldString, ' ', ',', [rfReplaceAll]);
然后将结果导入TStringList:
var
MyStringList : TStringList;
begin
MyStringList := TStringList.Create;
try
MyStringList.CommaText := StringReplace(oldString, ' ', ',', [rfReplaceAll]);
Result := MyStringList.Values['email'];
finally
MyStringList.Free;
end;
end;
这将为您提供电子邮件价值。然后需要在“@”符号处拆分字符串,这是一个相对简单的练习。当然,这只有在空格真正是字段之间的分隔符时才有效
或者,您可以使用正则表达式,但Delphi本身不支持正则表达式(您需要一个正则表达式库-请参阅)
***Smasher注意到(D2006+)分隔符/分隔文本,看起来像这样:
MyStringList.Delimiter := ' ';
MyStringList.DelimitedText := oldString;
Result := MyStringList.Values['email'];
将字符串拆分为一个字符串数组,使用“=”作为除数,然后将有一个按以下顺序排列的数组:'Key'然后'Value',然后可以循环查找'email'键,然后只需在数组索引中添加1即可获得值。但这可能会在很多方面失败(例如,有人输入“=”作为字符),或者值字段中有空字符串。另一个想法是,您也可以将PosEx(StrUtils)与StringList文本一起使用:
function ExtractMyString(SrcStr, FromStr, ToStr: string): string;
var
posBeg, posEnd: integer;
begin
Result := '';
posBeg := Pos(FromStr, SrcStr) + Length(FromStr);
posEnd := PosEx(ToStr, SrcStr, posBeg);
if (posBeg > 0) and (posEnd > posBeg) then
Result := Copy(SrcStr, posBeg, posEnd-posBeg);
end;
用法:
ExtractMyString(StringList.Text, 'email=', ' lots=');
当然,只有当源字符串的格式始终相同时,这才起作用,因为它便于根据需要提取其他数据。假设字符串保存在变量“s”中,并且“tmp”是另一个字符串变量
i:= pos ('email=', s);
tmp:= '';
inc (i);
while s[i] <> ' ' do
begin
tmp:= tmp + s[i];
inc (i);
end;
i:=pos('email=',s);
tmp:='';
公司(一);
而s[i]''是
开始
tmp:=tmp+s[i];
公司(一);
结束;
“tmp”将保留该地址为什么要投否决票?这是一个有效的问题,IMHO.yeah,但是你可以搜索email单词,跳过“=”,循环每个字符,直到你点击@…不,因为“email”可以出现在任何地方-在以前的值中,在以前的属性名称中…@Skuta那么你为什么不添加一个答案呢?你认为我们会否决你吗?+1很好的解决方案。不知道
值
属性。您可以使用StringList.Delimiter
和StringList.DelimitedText
而不是用逗号替换空格。CommaText属性不要求记录用逗号分隔。空白字符也可以,所以不需要将空格转换为逗号。不过,最好将StrictDelimiter设置为true时设置DelimitedText。然后允许在键或值中使用逗号。