在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这样的句中,
因为只有一个',那是不完整的字符串表达式,是无效的表达式,
所以必然产生解析异常;
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这样的句中,
因为只有一个',那是不完整的字符串表达式,是无效的表达式,
所以必然产生解析异常;
相关文章推荐
- Delphi中用状态图对字符串数据进行类型判断
- Delphi中用状态图对字符串数据进行类型判断
- Delphi解析类似\u97e9这样的Unicode字符串
- delphi的消息机制实现消息传递字符串
- redis源码解析1-简单动态字符串
- 解析字符串,每八位的二进制转为十进制
- JS解析XML文件和字符串的跨浏览器实现
- 使用Boost.Spirit实现四则运算字符串的解析
- Ext中解析字符串
- Delphi中的字符串比较(CompareStr)
- Volley扩展-json上传,json字符串解析
- delphi中的字符串操作函数的使用(上)
- java解析xml格式字符串
- Delphi的字符串与16进制的相互转换函数的汇编代码
- java解析json格式的字符串 maven依赖
- @功能 中用到 android EditText插入字符串到光标所在位置
- Java 字符串全面解析
- Delphi判断字符串中是否包含汉字,并返回汉字位置
- [置顶] cJSON库(构建json与解析json字符串)-c语言