全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1389
推到 Plurk!
推到 Facebook!

Delphi 使用regular express

尚未結案
syene
一般會員


發表:17
回覆:12
積分:5
註冊:2003-01-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-22 00:12:29 IP:220.138.xxx.xxx 未訂閱
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, VBScript_RegExp_55_TLB, ExtCtrls, StdCtrls; type TfrmRegExp = class(TForm, IRegExp2) Panel1: TPanel; mmoSrc: TMemo; Splitter1: TSplitter; mmoRst: TMemo; btnOpen: TButton; edtPattern: TEdit; dlgOpen: TOpenDialog; btnFindMatch: TButton; procedure FormCreate(Sender: TObject); procedure btnOpenClick(Sender: TObject); procedure btnFindMatchClick(Sender: TObject); private { Private declarations } FRegex:IRegExp2; public { Public declarations } {IRegExp2} function Get_Pattern: WideString; safecall; procedure Set_Pattern(const pPattern: WideString); safecall; function Get_IgnoreCase: WordBool; safecall; procedure Set_IgnoreCase(pIgnoreCase: WordBool); safecall; function Get_Global: WordBool; safecall; procedure Set_Global(pGlobal: WordBool); safecall; function Get_Multiline: WordBool; safecall; procedure Set_Multiline(pMultiline: WordBool); safecall; function Execute(const sourceString: WideString): IDispatch; safecall; function Test(const sourceString: WideString): WordBool; safecall; function Replace(const sourceString: WideString; replaceVar: OleVariant): WideString; safecall; property Pattern: WideString read Get_Pattern write Set_Pattern; property IgnoreCase: WordBool read Get_IgnoreCase write Set_IgnoreCase; property Global: WordBool read Get_Global write Set_Global; property Multiline: WordBool read Get_Multiline write Set_Multiline; end; var frmRegExp: TfrmRegExp; implementation {$R *.dfm} procedure TfrmRegExp.FormCreate(Sender: TObject); begin Fregex:=CoRegExp.Create; FRegex.IgnoreCase:=True; FRegex.Global:=True; FRegex.Multiline:=True; end; procedure TfrmRegExp.btnOpenClick(Sender: TObject); begin with dlgOpen do if Execute then mmoSrc.Lines.LoadFromFile(FileName); end; procedure TfrmRegExp.btnFindMatchClick(Sender: TObject); var i:Integer; MatchesCollection:IMatchCollection2; begin MatchesCollection:=CoMatchCollection.Create; FRegex.Pattern:=edtPattern.Text; MatchesCollection:= FRegex.Execute(mmoSrc.Text) as IMatchCollection2; for i:=1 to MatchesCollection.Count-1 do mmoRst.Lines.Add((MatchesCollection.Item[i] as IMatch2).Value); end; end. 嘗試使用VBScript_RegExp_55_TLB,來進行regular express, 在complie時,出現以下錯誤: [Error] Unit1.pas(28): Unsatisfied forward or external declaration: 'TfrmRegExp.Get_Pattern' [Error] Unit1.pas(29): Unsatisfied forward or external declaration: 'TfrmRegExp.Set_Pattern' [Error] Unit1.pas(30): Unsatisfied forward or external declaration: 'TfrmRegExp.Get_IgnoreCase' [Error] Unit1.pas(31): Unsatisfied forward or external declaration: 'TfrmRegExp.Set_IgnoreCase' [Error] Unit1.pas(32): Unsatisfied forward or external declaration: 'TfrmRegExp.Get_Global' [Error] Unit1.pas(33): Unsatisfied forward or external declaration: 'TfrmRegExp.Set_Global' [Error] Unit1.pas(34): Unsatisfied forward or external declaration: 'TfrmRegExp.Get_Multiline' [Error] Unit1.pas(35): Unsatisfied forward or external declaration: 'TfrmRegExp.Set_Multiline' [Error] Unit1.pas(36): Unsatisfied forward or external declaration: 'TfrmRegExp.Execute' [Error] Unit1.pas(37): Unsatisfied forward or external declaration: 'TfrmRegExp.Test' [Error] Unit1.pas(38): Unsatisfied forward or external declaration: 'TfrmRegExp.Replace' [Fatal Error] Regex.dpr(7): Could not compile used unit 'Unit1.pas' 請問該如何宣告這些CoClass的方法及屬性. 謝謝! 發表人 - syene 於 2005/04/22 00:17:01
wyndog
資深會員


發表:7
回覆:362
積分:348
註冊:2004-10-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-04-22 09:49:51 IP:60.248.xxx.xxx 未訂閱
1. 首先你要有 VBScript_RegExp_55_TLB, 顯然你有 :p    2. 試試在你的 Project 這樣寫:
uses
  comobj, VBScript_RegExp_55_TLB;    procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  msg: string;
  re: IRegExp;
  Matchs: IMatchCollection;
  Match: IMatch;
begin
  re := CreateComObject(CLASS_RegExp) as IRegExp;
  re.Pattern := '[0-9]';
  if re.Test('0') then
    ShowMessage('Match');      re.Pattern := 'is.';
  re.IgnoreCase := True;
  re.Global := True;
  Matchs := re.Execute('IS1 is2 IS3 is4') as IMatchCollection;      msg := '';
  for i := 0 to Matchs.Count - 1 do begin
    Match := Matchs.Item[i] as IMatch;
    msg := msg   IntToStr(i)   ':'   Match.Value   #13   #10;
  end;
  ShowMessage(msg);
end;
系統時間:2024-05-18 7:24:23
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!