有一段字符串内容如下:
itemlist[0]=new ListItem("L1","key1","name1","null",0,true,true);itemlist[1]=new ListItem("L2","key2","name2","L1",1,false,false);itemlist[2]=new ListItem("L3","key3","name3","L1",1,false,false);itemlist[3]=new ListItem("L4","key4","name4","null",0,true,true);请问下怎么查找取出每个双引号里面的字符;能得到如下结果,谢谢!
L1 Key1 name1 null
L2 Key2 name2 L1
L3 Key3 name3 L1
L4 Key4 name4 null
itemlist[0]=new ListItem("L1","key1","name1","null",0,true,true);itemlist[1]=new ListItem("L2","key2","name2","L1",1,false,false);itemlist[2]=new ListItem("L3","key3","name3","L1",1,false,false);itemlist[3]=new ListItem("L4","key4","name4","null",0,true,true);请问下怎么查找取出每个双引号里面的字符;能得到如下结果,谢谢!
L1 Key1 name1 null
L2 Key2 name2 L1
L3 Key3 name3 L1
L4 Key4 name4 null
stemp="";
while(data_buf[j++]!='"')
stemp+=data_buf[j-1];
const
Head = 'ListItem(';
Tail = ');';
var
A : TStringList;
S : String;
i, j: Integer;
begin
S := 'itemlist[0]=new ListItem("L1","key1","name1","null",0,true,true);' +
'itemlist[1]=new ListItem("L2","key2","name2","L1",1,false,false);' +
'itemlist[2]=new ListItem("L3","key3","name3","L1",1,false,false);' +
'itemlist[3]=new ListItem("L4","key4","name4","null",0,true,true);';
A := TStringList.Create;
try
A.Delimiter := ',';
i := PosEx(Head, S);
while i > 0 do
begin
j := PosEx(Tail, S, i + Length(Head));
if j <= 0 then break;
A.DelimitedText := copy(s, i + Length(Head), j - i - Length(Head));
Memo1.Lines.Add(Format('%s %s %s %s', [A.Strings[0], A.Strings[1], A.Strings[2], A.Strings[3]]));
i := PosEx(Head, S, j + Length(Tail));
end;
finally
A.Free;
end;
end;
需要这一行, 上面漏发了:
uses StrUtils;
{学了一下正则表达式以及相关组件TPerlRegEx}
procedure TForm1.Button1Click(Sender: TObject);
begin
with TPerlRegEx.Create(nil) do begin
Subject := 'new ListItem("L1","key1","name1","null",0,true,true);';
RegEx := '(".*?",)';
while MatchAgain do
Memo1.Lines.Add(MatchedExpression);
Free;
end;
end;
{再复杂一些的我就写不出了。^_^}
ls, ls2: TStringList;
i: Integer;
begin
ls := TStringList.Create;
ls.Add('itemlist[0]=new ListItem("L1","key1","name1","null",0,true,true);');
ls.Add('itemlist[1]=new ListItem("L2","key2","name2","L1",1,false,false);');
ls.Add('itemlist[2]=new ListItem("L3","key3","name3","L1",1,false,false);');
ls.add('itemlist[3]=new ListItem("L4","key4","name4","null",0,true,true);'); for i := 0 to ls.Count -1 do
begin
ls[i] := Copy(ls[i],LastDelimiter('(',ls[i])+1,999);
ls[i] := Copy(ls[i],1,LastDelimiter(')',ls[i])-1); ls2 := TStringList.Create;
ls2.Delimiter := ',';
ls2.DelimitedText := ls[i]; ls2[0] //拿来用做你的事
ls2[1]
ls2[2]
ls2[3]
ls2[4]
ls2.Free;
end; ls.Free;
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
ListBox2: TListBox;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function ConvertLine(s: string): string;
procedure ConvertStrings(ls: TStrings; destls: TStrings);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Items.Add('itemlist[0]=new ListItem("L1","key1","name1","null",0,true,true);');
ListBox1.Items.Add('itemlist[1]=new ListItem("L2","key2","name2","L1",1,false,false);');
ListBox1.Items.Add('itemlist[2]=new ListItem("L3","key3","name3","L1",1,false,false);');
ListBox1.Items.Add('itemlist[3]=new ListItem("L4","key4","name4","null",0,true,true);');
end;function GetString(var s: string): string;
var
p: integer;
begin
p := Pos('"', s);
if p > 0 then
begin
Delete(s, 1, p);
p := Pos('"', s);
if p > 0 then
begin
Result := Copy(s, 1, p-1);
Delete(s, 1, p);
Exit;
end;
end;
Result := '';
end;function TForm1.ConvertLine(s: string): string;
begin
Result := GetString(s) + ' ' + GetString(s) + ' ' + GetString(s) + ' ' + GetString(s);
end;procedure TForm1.ConvertStrings(ls: TStrings; destls: TStrings);
var
i: integer;
begin
for i := 0 to ls.Count-1 do
begin
destls.Add(ConvertLine(ls[i])); end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ConvertStrings(ListBox1.Items, ListBox2.Items);
end;end.