如何从 StringList 中解析多行
我想从 StringList 复制特定行, 我想将所有具有“域状态:”的行复制到 memo.lines.text 中 我使用了下面的代码,但问题是它只复制第一行,我想复制具有“域状态:”的所有行:
const
FieldNames: array[0..2] of string = ('Domain Status', 'domain status', 'Domain status');
begin
sl := TStringList.Create;
try
sl.Assign(Memo.Lines);
for I := 0 to sl.Count-1 do begin
sl[I] := TrimLeft(sl[I]);
end;
sl.NameValueSeparator := ':';
for I := Low(FieldNames) to High(FieldNames) do begin
status := Trim(sl.Values[FieldNames[I]]);
if status <> '' then Break;
end;
finally
sl.Free;
end;
memo1.lines.text:=status;
StringList 中的文本示例:
Domain Name: yahoo.com
Registry Domain ID: 3643624_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.markmonitor.com
Registrar URL: http://www.markmonitor.com
Updated Date: 2022-03-09T15:51:45+0000
Creation Date: 1995-01-18T08:00:00+0000
Registrar Registration Expiration Date: 2023-01-19T05:00:00+0000
Registrar: MarkMonitor, Inc.
Registrar IANA ID: 292
Registrar Abuse Contact Email: [email protected]
Registrar Abuse Contact Phone: +1.2083895770
Domain Status: clientUpdateProhibited (https://www.icann.org/epp#clientUpdateProhibited)
Domain Status: clientTransferProhibited (https://www.icann.org/epp#clientTransferProhibited)
Domain Status: clientDeleteProhibited (https://www.icann.org/epp#clientDeleteProhibited)
Domain Status: serverUpdateProhibited (https://www.icann.org/epp#serverUpdateProhibited)
Domain Status: serverTransferProhibited (https://www.icann.org/epp#serverTransferProhibited)
Domain Status: serverDeleteProhibited (https://www.icann.org/epp#serverDeleteProhibited)
Registrant Organization: Yahoo Assets LLC
Registrant State/Province: VA
我想得到:
clientUpdateProhibited
clientTransferProhibited
clientDeleteProhibited
serverUpdateProhibited
serverTransferProhibited
.....
without the http://www.icann.org...
I want to copy specific lines from a StringList,
I want to copy all lines that have 'Domain Status:' into memo.lines.text
I used the code below, but the problem is it copies only the first line, I want to copy all lines that have 'Domain Status:':
const
FieldNames: array[0..2] of string = ('Domain Status', 'domain status', 'Domain status');
begin
sl := TStringList.Create;
try
sl.Assign(Memo.Lines);
for I := 0 to sl.Count-1 do begin
sl[I] := TrimLeft(sl[I]);
end;
sl.NameValueSeparator := ':';
for I := Low(FieldNames) to High(FieldNames) do begin
status := Trim(sl.Values[FieldNames[I]]);
if status <> '' then Break;
end;
finally
sl.Free;
end;
memo1.lines.text:=status;
Example of text in the StringList :
Domain Name: yahoo.com
Registry Domain ID: 3643624_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.markmonitor.com
Registrar URL: http://www.markmonitor.com
Updated Date: 2022-03-09T15:51:45+0000
Creation Date: 1995-01-18T08:00:00+0000
Registrar Registration Expiration Date: 2023-01-19T05:00:00+0000
Registrar: MarkMonitor, Inc.
Registrar IANA ID: 292
Registrar Abuse Contact Email: [email protected]
Registrar Abuse Contact Phone: +1.2083895770
Domain Status: clientUpdateProhibited (https://www.icann.org/epp#clientUpdateProhibited)
Domain Status: clientTransferProhibited (https://www.icann.org/epp#clientTransferProhibited)
Domain Status: clientDeleteProhibited (https://www.icann.org/epp#clientDeleteProhibited)
Domain Status: serverUpdateProhibited (https://www.icann.org/epp#serverUpdateProhibited)
Domain Status: serverTransferProhibited (https://www.icann.org/epp#serverTransferProhibited)
Domain Status: serverDeleteProhibited (https://www.icann.org/epp#serverDeleteProhibited)
Registrant Organization: Yahoo Assets LLC
Registrant State/Province: VA
I would like to get :
clientUpdateProhibited
clientTransferProhibited
clientDeleteProhibited
serverUpdateProhibited
serverTransferProhibited
.....
without the http://www.icann.org...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要循环遍历
TStringList
的各个字符串,TStringList.Values[]
属性不会帮助您完成此任务,因为它只会搜索第一个字符串有一个匹配的名字。不过,您可以使用TStringList.Names[]
和TStringList.ValueFromIndex[]
属性来帮助您。此外,您根本不需要
FieldNames[]
数组。请使用不区分大小写的比较,例如SysUtils.SameText()
。尝试更多类似这样的事情:
You need to loop through the individual strings of the
TStringList
, theTStringList.Values[]
property will not help you with this task, as it will only search for the 1st string with a matching name. You can, however, use theTStringList.Names[]
andTStringList.ValueFromIndex[]
properties to help you.Also, you don't need the
FieldNames[]
array at all. Use a case-insensitive comparison, likeSysUtils.SameText()
instead.Try something more like this:
另一种方法是使用正则表达式。有点过分,但是如果您可以保持正则表达方式而不是使用每种使用性能重新创建它,则可以是可比的。
Another approach is to use regular expressions. A bit overkill but if you can keep the regular expression around instead of recreating it with each use performance can be comparable.