您的位置:首页 > 编程语言 > Delphi

在delphi中用TParser 类解析字符串

2006-10-22 10:23 417 查看
BDN参考的写法:http://bdn.borland.com/article/26380

procedure TForm1.Button6Click(Sender: TObject);
var
theParser: TParser;
oString: TStringStream;
str:AnsiString;
Pos,Line:integer;
begin
oString := TStringStream.Create(Memo1.Lines.text);
oString.Position := 0;
theParser:= TParser.Create(oString);

while (theParser.Token <> toEOF) do //while we're in the file
begin
//Get Token
str:=theParser.TokenString();
//Get the position in the stream
Pos:=theParser.SourcePos();
//Get the line number
Line:=theParser.SourceLine;
//Get token type
case (theParser.Token) of
toSymbol:
Memo2.Lines.Add(str+' is a symbol at line : '+IntToStr(Line)+' position : '+IntToStr(Pos));

toInteger:
Memo2.Lines.Add(str+' is an integer at line : '+IntToStr(Line)+' position : '+IntToStr(Pos));

toFloat:
Memo2.Lines.Add(str+' is an float at line : '+IntToStr(Line)+' position : '+IntToStr(Pos));

toString:
Memo2.Lines.Add(str+' is an string at line : '+IntToStr(Line)+' position : '+IntToStr(Pos));
end;
theParser.NextToken();
end;
theParser.Free;
oString.Free;

end;

==========================================================================
当用于词频统计时,zswang(伴水清清)(专家门诊清洁工) 提供了一个写法
参考:http://community.csdn.net/Expert/TopicView3.asp?id=5097298
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
vStringStream: TStringStream;
vWords: TStringList;
vStr: string;
begin
I := 0;
vStringStream := TStringStream.Create(Memo1.Lines.text);
vWords := TStringList.Create;
with TParser.Create(vStringStream) do try
vWords.Sorted := True;
while(Token <> toEOF) do
begin
if TokenSymbolIs(TokenString) then
begin
vStr := TokenString;
if vWords.IndexOf(vStr) < 0 then // 如果单词已经添加了
vWords.Add(vStr);
Inc(I);
end;
NextToken;
end;

Label1.Caption := Format('单词共: %d个', [I]);
Label2.Caption := Format('不同的单词共: %d个', [vWords.Count]);
//show Memo2.Lines.Assign(vWords);
finally
vWords.Free;
Free;
vStringStream.Free;
end;
end;

==========================================================================
注:TParser类,解析字符串时,若字符串中只有一个',会产生异常,提示(Invalid string constant on line 1),
这是因为TParser这个为DFM解析而写的类,关于'号的处理,有有特殊要求的,那就是要成对出现,并且两个''之字符,将认为是字符串.

也就是说,在用来解析普通字符串时,会因为如Teacher's day这样的句中,
因为只有一个',那是不完整的字符串表达式,是无效的表达式,
所以必然产生解析异常;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: