function Get(var S:string;R:Tstrings):boolean; //
var TmpStr:string;
i,cnt:integer;
begin
result:=true;
R.Clear;
while s<>'' do
begin
TmpStr:='';
cnt:=0;
repeat
if s[1]='(' then cnt:=cnt+1;
delete(s,1,1);
if s='' then
begin
result:=true;
exit;
end;
until s[1]='(';
while true do
begin
if s='' then
begin
result:=false;
exit;
end;
TmpStr:=TmpStr+s[1];
if s[1]=')' then
begin
cnt:=cnt-1;
if cnt<0 then
begin
result:=true;
break;
end;
end;
delete(s,1,1);
end;
R.Add(TmpStr);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
begin
s:='((a=1 and b=2) and c=1) or (d=1 and e=1)';
get(s,memo1.Lines);
end;