2011-02-10 55 views

回答

2

如果你是快樂與天真的解決方案,看看這個SO:Extract string from a text file using 2 delimiters

如果您想要進行代碼分析,您實際上需要一個完整的語言解析器capa理解評論,條件編譯+所有其他預編譯器選項。

2
uses 
    StrUtils; 
... 
const 
    sBegin = 'begin'; 
    sEnd = 'end'; 
var 
    i1, i2: Integer; 
    sSubstr: String; 
... 
    i1 := Pos(sBegin, s); 
    if i > 0 then begin 
    i2 := PosEx(sEnd, s, i1 + Length(sBegin)); 
    if i2 > 0 then begin 
     sSubstr := Copy(s, i1 + Length(sBegin), i2 - (i1 + Length(sBegin))); 
     // process the delimited substring 
    end; 
    end; 

注:

  1. 上面的代碼沒有被證實。
  2. 類似的可以由正則表達式完成,或者由C/C++ sscanf限制。
1

Extract string from a text file using 2 delimiters

複製功能,並調用它像這樣:

`ExtractText('Hi my name is$John and I''m happy/today','$','/')`; 

它會返回 '約翰和I''m快樂。'

例子:

ShowMessage(ExtractText('Hello,World!', ',' ,'!')); 

它會在一個彈出顯示的 '世界'。